NodeJS Promises无法编辑现有的'模型'

时间:2016-10-17 16:20:16

标签: javascript mysql node.js promise bluebird

修改 仔细检查后,async.forEach似乎在getCurrencies第一次运行之前完成......我该如何解决这个问题?我认为回复一个承诺基本上会链接'这些承诺在一起吗?

我有以下代码,它试图从mysql读取事务。由于表的大小和查询所花费的时间长度,我打破它并使用promises来填充对象。但是,getCurrencies并未更新模型currencyCode - 但是,console.log都报告了正确的值。有人可以解释一下我做错了吗?我仍然对承诺很陌生。感谢。

主要对象构建

    connection.query(query).then(function(items) {
        async.forEach(items, function(element) {
            element.ptype = 'merchants';
            element.type = 'transactions';
            element.currencyCode = null;

            getCurrencies(element.currencies_id).then(function(result) {
                console.log(result.currencyCode);
                element.currencyCode = result.currencyCode;
                console.log(JSON.stringify(element));
            });
        });

        res.send(items);
    });

getCurrencies

function getCurrencies(transactionId) {
    var currencies_query = '';
    currencies_query += 'SELECT ';
    currencies_query += 'iso_code AS currencyCode, ';
    currencies_query += 'name AS `currencyName`, ';
    currencies_query += 'symbol AS `currencySymbol`, ';
    currencies_query += 'exponent AS `currencyExponent`, ';
    currencies_query += 'iso_num AS `currencyNumber` ';
    currencies_query += 'FROM ';
    currencies_query += 'currencies ';
    currencies_query += 'WHERE ';
    currencies_query += 'id = ' + transactionId;

    return new Promise(function(resolve, reject) {
        connection.query(currencies_query).then(function(rows) {
            resolve(rows[0]);
        });
    });
};

来自console.log s

的输出

第一

GBP

第二

{"id":11213595,"pid":100856,"uri":"/rest/transactions/11213595","puri":"/rest/merchants/100856","path":"/resellers/1/customers/5/merchants/100856/transactions/11213595","transactionID":11213595,"currencies_id":1,"transactionUri":"/rest/transactions/11213595","ptype":"merchants","type":"transactions","currencyCode":"GBP"}

从邮递员那里得到api回复

{
    "id": 11213615,
    "pid": 103845,
    "uri": "/rest/transactions/11213615",
    "puri": "/rest/merchants/103845",
    "path": "/resellers/1/customers/2368/merchants/103845/transactions/11213615",
    "transactionID": 11213615,
    "currencies_id": 1,
    "transactionUri": "/rest/transactions/11213615",
    "ptype": "merchants",
    "type": "transactions",
    "currencyCode": null
  }

正如您所看到的,currencyCode仍然是null - 为什么?! :(

1 个答案:

答案 0 :(得分:1)

  

如您所见,currencyCode仍然为空 - 为什么?! :(

我没有使用" async.forEach"之前我认为,currencyCode是空的 - 因为你称之为承诺" getCurrencies",它是异步工​​作的

如果你需要回应客户的工作结果很少承诺 - 你必须收集数组的承诺,然后调用 Promise.all (这将返回所有承诺的结果数组)< / p>

你应该在下一个链块中发送响应。 我认为它应该有用

connection.query(query)
.then(function(items) {
    var arPromises = [];
    async.forEach(items, function(element) {
        element.ptype = 'merchants';
        element.type = 'transactions';
        element.currencyCode = null;
        arPromises.push(
            new Promise(function (resolve, reject) {
                getCurrencies(element.currencies_id)
                    .then(function(result) {
                        element.currencyCode = result.currencyCode;
                        resolve(element);
                    }).catch(function (error) {
                        //do something with error
                        if(error)
                           reject(error);
                    });
            })
        );
    });
    return Promise.all(arPromises);
}).then(function(items){
    res.send(items);
}).catch(function (error) {
    if(error)
     console.log(error);
});

我的建议,完成与#34;赶上&#34;阻止 - 以避免丢失错误