I'm having a problem with a node.js script at work (I'm pretty new to node in general, btw.). When I run through the forEach loop, it appears that it might be running asynchronously and therefor is not running problem. However, when I looked through the package documentation, I didn't see anything about get the sql execution to run synchronously. Thanks in advance for the help!
function getLastLoginAndProviderType(jsonObj, connection){
console.log(jsonObj);
jsonObj.forEach(obj => {
console.log("Processing: " + obj["email"]);
//obj["email"], obj["first"], obj["last"], obj["eid"], obj["role"]
var sql = "select LAST_LOGGED_IN_TIME from EINV_OWNER.CONTACT where EMAIL_ADDRESS='" + obj['email'] + "'";
connection.execute(
sql, {}, { maxRows: 15 },
function(err, result) {
if (err) {
console.error("Query Error: " + err.message);
} else {
console.log("Number of rows returned: " + result.rows.length);
obj["lastLoggedIn"] = "null";
obj["supplierOrProvider"] = "null";
}
});
});
When the script runs, I would expect to see something like:
Processing: XXX@XXX.com
Number of rows returned: XX
Processing: XXX@XXX.com
Number of rows returned: XX
...
Processing: XXX@XXX.com
Number of rows returned: XX
However, what I end up getting is:
Processing: XXX@XXX.com
Processing: XXX@XXX.com
Processing: XXX@XXX.com
...
Processing: XXX@XXX.com
Query Error: NJS-003: invalid connection
Query Error: NJS-003: invalid connection
Query Error: NJS-003: invalid connection
...
Query Error: NJS-003: invalid connection
Any ideas? Thanks!