使用pg-promises嵌套查询

时间:2017-08-23 11:08:34

标签: node.js express pg-promise

我需要使用pg-promise使用结果进行查询以进行其他3次查询,但我在执行时遇到此错误:

  

未处理拒绝TypeError:方法'批次'需要一个数组   值。       批处理(C:\ apps \ pfc \ node_modules \ spex \ lib \ ext \ batch.js:61:26)       在C:\ apps \ pfc \ node_modules \ spex \ lib \ ext \ batch.js:149:26       在Task.batch(C:\ apps \ pfc \ node_modules \ pg-promise \ lib \ task.js:120:39)..............

这是我的代码:

db.task(t => {
    return t.one('select gid, idgrupo from orden where gid = $1', req.params.ordenid, resultado => {
        return t.batch([
            t.one('select o.gid as num, v.matricula, v.denom, o.pkini, o.pkfin, o.fechaini, o.f_estimada, o.fechafin, o.idestado, o.descr, o.instr, g.id as idgrupo, g.nombre as grupo, g.actividad, e.descr as estado from orden as o inner join estado as e on o.idestado = e.gid inner join grupo as g on o.idgrupo = g.id inner join via as v on o.idctra = v.gid and o.gid = $1', resultado.gid),
            t.any('select * from operacion order by gid where idgrupo = $1', resultado.idgrupo),
            t.any('select m.gid, m.fechamed as fecha, m.cantidad, m.costemed as coste, o.codigo, o.descr from medicion m, operacion o where m.idorden = $1 and m.idope = o.gid order by fecha asc', resultado.gid)
        ])
            .then(data => {
                res.render('mediciones/nuevaMed', {
                        title: 'Crear / Modificar parte de trabajo',
                        orden: data[0],
                        operaciones: data[1],
                        medicion: [],
                        mediciones: data[2],
                        errors: []
                });
            }).catch(function(error) {next(error);});
    }).then(t.batch);
 });

1 个答案:

答案 0 :(得分:3)

我是pg-promise的作者。

您的代码存在一些问题,如下所述......

  • 与方法one一起使用的值转换回调用于转换返回的值。即使从技术上讲,你可以返回一个承诺,但是从承诺的角度来看,这会产生一个尴尬的代码。我建议不要这样做。

  • 将非数据库代码放入数据库任务中并不是一个好的解决方案,这会创建更难以维护的混合用途代码,并且也被视为反模式。

  • 你最终得到的错误是因为你.thenbatch的结果进行了undefined,在你的代码中会db.task(t => { return t.one('select gid, idgrupo from orden where gid = $1', req.params.ordenid) .then(resultado => { return t.batch([ t.one('select o.gid as num, v.matricula, v.denom, o.pkini, o.pkfin, o.fechaini, o.f_estimada, o.fechafin, o.idestado, o.descr, o.instr, g.id as idgrupo, g.nombre as grupo, g.actividad, e.descr as estado from orden as o inner join estado as e on o.idestado = e.gid inner join grupo as g on o.idgrupo = g.id inner join via as v on o.idctra = v.gid and o.gid = $1', resultado.gid), t.any('select * from operacion order by gid where idgrupo = $1', resultado.idgrupo), t.any('select m.gid, m.fechamed as fecha, m.cantidad, m.costemed as coste, o.codigo, o.descr from medicion m, operacion o where m.idorden = $1 and m.idope = o.gid order by fecha asc', resultado.gid) ]); }); }) .then(data => { res.render('mediciones/nuevaMed', { title: 'Crear / Modificar parte de trabajo', orden: data[0], operaciones: data[1], medicion: [], mediciones: data[2], errors: [] }); }) .catch(next); 传递它进入另一个batch,显然不喜欢它并抛出那个错误。问题是,你根本不需要它。您必须从需要的代码中复制if,并将其放在不需要的地方:)

说了这么多,这就是你的代码应该是什么样子:

db.task(function* (t) {
    const resultado = yield t.one('select gid, idgrupo from orden where gid = $1', req.params.ordenid);
    const orden = yield t.one('select o.gid as num, v.matricula, v.denom, o.pkini, o.pkfin, o.fechaini, o.f_estimada, o.fechafin, o.idestado, o.descr, o.instr, g.id as idgrupo, g.nombre as grupo, g.actividad, e.descr as estado from orden as o inner join estado as e on o.idestado = e.gid inner join grupo as g on o.idgrupo = g.id inner join via as v on o.idctra = v.gid and o.gid = $1', resultado.gid);
    const operaciones = yield t.any('select * from operacion order by gid where idgrupo = $1', resultado.idgrupo);
    const mediciones = yield t.any('select m.gid, m.fechamed as fecha, m.cantidad, m.costemed as coste, o.codigo, o.descr from medicion m, operacion o where m.idorden = $1 and m.idope = o.gid order by fecha asc', resultado.gid);
    return {orden, operaciones, mediciones};
})
    .then(data => {
        res.render('mediciones/nuevaMed', {
            title: 'Crear / Modificar parte de trabajo',
            orden: data.orden,
            operaciones: data.operaciones,
            medicion: [],
            mediciones: data.mediciones,
            errors: []
        });
    })
    .catch(next);

使用ES6 Generators语法时更简单:

function FormStore () {

    this.setup = function(){
        this.store = {};
        this.ERR_LINE_PREFIX = '#err_';
        this.NO_DISPLAY_CLASS = 'no-display';
        this.settings = {
            'myID':{'hide':false},
        }
    }

    this.checkVal= function(){
        var geoArr = ['id_xx','myID'];
        var id;
        $.each( geoArr, function(val) {
            id = geoArr[val];
            console.log(this.store) //-> returns undefined, below line is error
            if (!(this.store[id])) { 
                return false;
            }

        });
    }
};
var FS = new FormStore();
FS.setup();