Plutonus Commissions
Plutonus Commissions

Reputation: 41

Variables Not Being Retained

Alrighty.

My problem is with the Javascript Objects itemList and priceList - they are not retained properly into the .then() section.

Here's the part that I'm having trouble with:

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);
});

And here's the important bit that it outputs:

Outputs

I've tried using strings instead of Javascript Objects, but it doesn't seem to like that either, and it outputs the same.

The 5 and undefined part should resemble the bit above it, but it doesn't.

Might it have something to do with the null parts? If so, will that mean it won't work with true - false - undefined etc?

Upvotes: 1

Views: 36

Answers (2)

Parijat Purohit
Parijat Purohit

Reputation: 921

remove the arguments itemList and priceList and your code should just be fine.

The itemList you are referring inside the .then function is actually what is resolved by the SQL promise.

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

Upvotes: 1

Prakash Sharma
Prakash Sharma

Reputation: 16482

You are using itemList and priceList of global scope so don't redefine them in local scope of then. Just remove those arguments from then

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

Upvotes: 1

Related Questions