variables不被保留

Alrighty。

我的问题是与Javascript对象itemListpriceList – 他们没有妥善保存到priceList .then()部分。

以下是我遇到的问题:

 var stmt = `SELECT * FROM SRV${msg.guild.id}`; var itemList = {}; var priceList = {}; var num = -1; sql.each(stmt, function(err, row) { num++ if(row.items!="ITEMS") { itemList[num] = row.items; } if(row.prices!="PRICES") { priceList[num] = row.prices; } console.log("---------\n" + JSON.stringify(itemList, null, 4) + "\n" + JSON.stringify(priceList, null, 4)); }) .then((itemList, priceList) => { console.log("----E----\n" + JSON.stringify(itemList, null, 4) + "\n" + JSON.stringify(priceList, null, 4) + "\n----E----"); let cmd = require("./itemsPROCESS.js"); cmd.run(transfer, msg, args, itemList, priceList); }); 

这是它输出的重要部分:

输出

我试过使用string,而不是Javascript对象,但它似乎并不喜欢,也输出相同。

5undefined部分应该类似于它上面的位,但它不。

这可能与零部件有关吗? 如果是这样,这是否意味着它将无法truefalseundefined等?

删除参数itemListpriceList ,你的代码应该没问题。

你在itemList函数中引用的itemList实际上是由SQL承诺解决的。

 .then(() => {...}); 

您正在使用全局范围的itemListpriceList ,因此不要在当地的范围内重新定义它们。 从那里删除那些参数

 .then(() => { console.log("----E----\n" + JSON.stringify(itemList, null, 4) + "\n" + JSON.stringify(priceList, null, 4) + "\n----E----"); let cmd = require("./itemsPROCESS.js"); cmd.run(transfer, msg, args, itemList, priceList); });