添加插入查询时承诺停止工作 [英] Promises stop working when adding a insertion query

查看:50
本文介绍了添加插入查询时承诺停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

询问节点的 Q 库的承诺,mido 帮助我解决了执行承诺的问题 在此线程中.基本上,解决方案是(几乎)这段代码.

Asking about promises with Q library for node, mido helped me with a problem with the execution of promises in this thread. Basically, the solution was (almost) this code.

    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 并将其传递给其他人.使用 Node.js 的 mySql 库可以轻松获取插入行的 id.这是承诺形式Form:

But now I need to generate an id in the first promise (Form.insert) and pass it throw the others. It's easy to have the id of the inserted row using mySql library for Node. This is the promise form 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查询,第一个promise在进程结束时执行,再次失败:

But now, with the INSERT INTOquery, the first promise is executed at the end of the process, doing it fails again:

//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 被定义为一个承诺,并且在插入数据后设置返回值(解析和拒绝),所以流程应该等待它的解析.

I don't understain why this occurs because Form.insert is defined as a promise and return values (resolve and reject) are set after inserting the data, so flow should wait for its resolution.

谢谢!

推荐答案

如果你想将第一个 promise 的 id 传递给其他人,你可以这样做:

if you want to pass the id from first promise to others, you can do something like:

...
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)
  ]))
}
...

这篇关于添加插入查询时承诺停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆