添加插入查询时Promise停止工作

时间:2016-05-26 09:09:16

标签: node.js promise q

询问有关节点的Q库的承诺,mido帮助我执行了promises in this thread的问题。基本上,解决方案是(差不多)这段代码。

    var form= [
        {'name':'FORM_NAME_1.1',
         'label2':'FORM_LABEL_1.2'
        },
        {'name':'FORM_NAME_2.1',
         'label2':'FORM_LABEL_2.2'
        }
    ];
    var params = ['params1','params2'];
    var meta   = ['meta1','meta2'];

    let process = (currentValue,index,arr) => {
        //let reqCopy = {id: Math.random()}
        let reqCopy = {};
        for(let attr in req)  // copy all the request attributes 
            //if(attr && attr!='id')
                reqCopy[attr] = req[attr]
        return Q.all([
            Form.insert(connection,req,form[index],reqCopy),
            Field.insert(connection,req,params[index],reqCopy),
            Meta.insert(connection,req,meta[index],reqCopy)
        ])
    }
    return Q.all(form.map(process))
    .catch((err) => console.log(err))
    .done(() => console.log('Done'));

但是现在我需要在第一个承诺 (Form.insert) 中生成一个id并将其传递给其他承诺。使用mySql库为Node获取插入行的id很容易。这是承诺表单Form

var Q = require('Q');

module.exports = {
    insert: (connection,req,dataToInsert,reqCopy) => {
        var deferred = Q.defer()
          , db       = 'DATABASE';

        console.log('Form.insert is executing with data: ' + JSON.stringify(dataToInsert));

    connection.query(`INSERT INTO ${db} SET ?`
        ,[dataToInsert]
        ,function(err, result) {
            if (err) deferred.reject(err);
            console.log('Result: ' + result.insertId);
            deferred.resolve(result.insertId); //Return id when data was inserted
        });

        return deferred.promise;
    }
}

但是现在,使用INSERT INTO查询,第一个承诺在流程结束时执行,再次失败:

//Promises are executed in the right order set in the .all array:
Form.insert is executing with data: {"name":"crf_NAME_1.1","label2":"FORM_LABEL_1.2"}
Field.insert is executing with data: "params1"
Meta.insert is executing with data: "meta1"
Form.insert is executing with data: {"name":"FORM_NAME_2.1","label2":"FORM_LABEL_2.2"}
Field.insert is executing with data: "params2"
Meta.insert is executing with data: "meta2"

//But Form.insert should generate the id before passing to the next promise.
C:\node\api\models\Form.js:17
            console.log('Result: ' + result.insertId); //IT FAILS, SHOULD BE EXECUTED BEFORE

我不明白为什么会发生这种情况,因为Form.insert被定义为一个promise,并且在插入数据后设置了返回值(resolve和reject),因此flow应该等待它的解析。

谢谢!

2 个答案:

答案 0 :(得分:1)

如果操作彼此独立,则AFAIK Q.all()非常有用。

如果依赖项,则可以使用常规的Promise链接:

return Form .insert(connection,req,form[index],reqCopy)
            .then((insertId) => {
              return Q.all([
                Field.insert(connection, req, insertId, params[index], reqCopy), 
                Meta .insert(connection, req, insertId, meta[index], reqCopy)
              ])
            })

这假设Field.insert()Meta.insert()仍然相互独立;如果没有,你可以使用相同的技巧将数据从前者传递给后者。我作为示例添加了insertId作为插入方法的额外参数。

答案 1 :(得分:1)

如果您想将... let process = (currentValue,index,arr) => { return Form.insert(connection,req,form[index]).then(id => Q.all([ Field.insert(connection,req,params[index],id), Meta.insert(connection,req,meta[index],id) ])) } ... 从第一个承诺传递给其他承诺,您可以执行以下操作:

/* No scrolling for collapsed menu */

.navbar-collapse.in {
    overflow: hidden;
    max-height: none !important;
    height: auto !important;
}