How to make this code execute in an appropriate order - node.js

I'm writting a web application in node.js which uses mongodb. I have this piece of code but unfortunatelly it does not execute in order, I mean for loops iterate faster than those db functions are executed and this makes little mess.
hospitalsCollection.update({}, {$pull: {doctors: parseInt(id)}}, function(err, success) {
treatmentsCollection.update({}, {$pull: {doctors: parseInt(id)}}, function(err, success) {
hospitalsCollection.find({"_id": {$in: hospitalsIds}}).toArray(function(err, hospitalsList) {
for(i = 0; i < hospitalsList.length; i++) {
for(j = 0; j < hospitalsList[i].treatments.length; j++) {
var exists = false;
for(k = 0; k < hospitalsList[i].doctors.length; k++) {
doctorsCollection.find({"_id": parseInt(hospitalsList[i].doctors[k])}).toArray(function(err, doctorObj) {
for(l = 0 ; l < doctorObj.treatments.length; l++) {
if(doctorObj.treatments[l] == hospitalsList[i].treatments[j]) {
exists = true;
break;
}
}
});
if(exists)
break;
}
if(exists) {
break;
}
else {
hospitalsCollection.update({"_id": parseInt(hospitalsList[i]._id)}, {$pull: {treatments: parseInt(hospitalsList[i].treatments[j])}}, function(err, success) {
treatmentsCollection.update({"_id": parseInt(hospitalsList[i].teratments[j])}, {$pull: {hospitals: parseInt(hospitalsList[i]._id)}}, function(err, success) {
console.log(err);
});
});
}
}
}
for(i = 0; i < treatments.length; i++) {
doctorsCollection.aggregate([ {$project:{"treatments._id":1, "treatments.price":1}}, {$unwind:"$treatments"},{$match:{"treatments._id": parseInt(treatments[i])}}, {$sort:{"treatments.price":-1}}, {$limit:1} ], function(err, result) {
doctorsCollection.aggregate([ {$project:{"treatments._id":1, "treatments.price":1}}, {$unwind:"$treatments"},{$match:{"treatments._id": parseInt(treatments[i])}}, {$sort:{"treatments.price":1}}, {$limit:1} ], function(err, result2) {
var maxPrice = result[0].treatments.price;
var minPrice = result2[0].treatments.price;
treatmentsCollection.update({"_id": parseInt(treatments[i])}, {$set: {"maxPrice": parseInt(maxPrice)}}, function(err, success) {
treatmentsCollection.update({"_id": parseInt(treatments[i])}, {$set: {"minPrice": parseInt(minPrice)}}, function(err, success) {
console.log(err);
});
});
});
});
}
});
});
});
I really don't know how to handle this. Any help would be appreciated.Thanks.

Use Promise (bluebird is great). And refactor your code.
With Promise, you can do something like below.
dbUpdate1()
.then(function(resultFromUpdate1) {
let multipleUpdates = []
multipleUpdates.push(updateCollectionA())
multipleUpdates.push(updateCollectionB())
return Promise.all(multipleUpdates)
.spread(afterUpdatingBothCollection)
function updateCollectionA(argument) {
// body...
}
function updateCollectionB(argument) {
// body...
}
function afterUpdatingBothCollection(resultFrom1, resultFrom2) {
// body...
}

Related

NodeJS Callback not returning completed array outside for loop

I've got an empty object array "storing" and I'm currently pushing objects in a nested for loop. This issue is when I call the callback(null, storing) outside the for loop it returns an empty array. Should I be running a promise call? Not sure where to go from here
var params = {
TableName: USER_TABLE,
KeyConditions: {
orgid: {
ComparisonOperator: 'EQ',
AttributeValueList: [org_uuid]
},
orgkey: {
ComparisonOperator: 'EQ',
AttributeValueList: [secret_key]
}
}
};
// query table for devices tied to user
dynamodb.query(params, function(err, data) {
if (err) {
badRequest();
} else {
// check whether correct org_uuid & org_key has been passed
var checked_org = data.Items[0].org_key;
if (checked_org == secret_key) {
var storing = [];
var value = data.Items[0].read.values;
for (var x = 0; x < value.length; x++) {
var query_params = {
TableName: DEVICE_TABLE,
KeyConditions: {
device_id: {
AttributeValueList: {
S: value[x]
},
ComparisonOperator: 'EQ'
}
}
};
dynamodb.query(query_params, function(err, data) {
if (err) {
console.log(err);
} else {
for (var i = 0; i < data.Count; i++) {
storing.push(data.Items[i].device_id);
}
}
});
}
callback(null, storing)
}
}
});
}

Nodejs how to access callback variable outside the function?

This is my code want to access callback variable newID outside calling-function. I want to insert bulk data into mongodb using batch with auto incremented id instead of default object
for (var i = 0; i < sizeOfResult; ++i) {
var newKey = {}; //Main json array
newKey = {
date: result[i]['date'],
issue: result[i]['issue'],
status: result[i]['status']
};
getNextSequenceValue("inventoryid",db, function(err, newID) {
newKey["_id"] = newID; <!-- try to add/assign callback variable(newID) into newKey -->
});
console.log("newKey: %j", newKey); <!-- but unable to get access callback variable(newID) here below-->
batch.insert(newKey);
}
// This is my called function
function getNextSequenceValue(name,db,callback) {
var ret = db.collection('counters_inv').findAndModify({ _id: name },null,{ $inc: { sequence_value: 1 } }, {new: true},
function(err,doc ) {
if(err){
return callback(err) // callback on error
}
callback(null, doc.value.sequence_value); // callback on success
});
}
Look at this code, you just need to put the variable outside and it works:
let i = 1
function functionwithcallback(callback) {
console.log(i)
i++
callback(i)
}
for (let j = 1; j <= 10; j++) {
functionwithcallback(() => {
if (j == 10)
console.log('done')
})
}
I'm not sure what the overall objective is, but the reason your newKey variable is not set correctly is because where it is being used has executed before the variable is set. In your example, your for loop is going to completely finish running kicking off a bunch of getNextSequenceValue() method calls that will eventually come back and run the callback code. It does not wait on the getNextSequenceValue function to finish before continuing the loop.
Solution: moving the console.log() and batch.insert() into the callback.
Here's an example that would execute in the correct order.
var keys = [];
for (var i = 0; i < sizeOfResult; ++i) {
var newKey = {
date: result[i]['date'],
issue: result[i]['issue'],
status: result[i]['status']
};
getNextSequenceValue("inventoryid", db, function(err, newID) {
newKey["_id"] = newID;
keys.push(newKey);
if (keys.length === sizeOfResult) {
console.log("keys: %j", keys);
batch.insertAll(keys);
}
});
}
function getNextSequenceValue(name, db, callback) {
db.collection('counters_inv').findAndModify({ _id: name }, null, { $inc: { sequence_value: 1 } }, {new: true},
function(err,doc) {
if(err){
return callback(err);
}
callback(null, doc.value.sequence_value);
});
}

How can get two collection documents and calculate points using express.js?

exports.show = = function(req, res) {
var userdata = [{
"productcode": "9563456789",
"cost": "1000"
}, {
"productcode": "8756348947",
"cost": "5600"
}]
var parameterObject = [];
Parameter.find().exec(function(err, Parameters) {
if (err) {
return handleError(res, err);
}
// i want to push Parameters[0].value to parameterObject
parameterObject.push({
pointvalue: Parameters[0].value
});
});
for (var i = 0; i < userdata.length; i++) {
Product.find({
'productcode': userdata[i].productcode
}).exec(function(err, Products) {
if (err) {
return handleError(res, err);
}
var point = 0;
if (!Products) {
point = 0;
} else if (Products[0].product.point > 0) {
point = Products[0].product.point;
}
if (point > 0) {
// here i am not getting userdata[i].cost
//parameterObject.pointvalue value also not getting
totalprice = userdata[i].cost / parameterObject.pointvalue * point;
}
});
}
};
Here i have written function for calculating totalprice. i have mentioned userdata(this is my req.body).
Expectation :
i need to store Parameters objects in some variable to access where ever i want.
i want to pass userdata object in Product.find() function
how can i calculate this
totalprice= userdata[i].cost/parameterObject.pointvalue) * point);
exports.show = = function(req, res) {
var userdata = [{
"productcode": "9563456789",
"cost": "1000"
}, {
"productcode": "8756348947",
"cost": "5600"
}]
var parameterObject = [];
Parameter.find().exec(function(err, Parameters) {
if (err) {
return handleError(res, err);
}
// i want to push Parameters[0].value to parameterObject
parameterObject.push({
pointvalue: Parameters[0].value
});
return FindProducts(parameterObject, function(data) {
console.log(data);
});
});
function FindProducts(parameterObject, callback) {
for (var i = 0; i < userdata.length; i++) {
var totalprice = 0;
findProduct(i, parameterObject, function(i, price) {
totalprice += price;
if (i <= userdata.length) {
return callback({
"userid": "myuserid",
"total": totalprice
});
}
});
}
}
function findProduct(i, parameterObject, callback) {
Product.find({
'productcode': userdata[i].productcode
}).exec(function(err, Products) {
if (err) {
return handleError(res, err);
}
var point = 0;
if (!Products) {
point = 0;
} else if (Products[0].product.point > 0) {
point = Products[0].product.point;
}
if (point > 0) {
// here you can now get the value of userdata[i].cost
// here you can now get the value of parameterObject
totalprice = userdata[i].cost / parameterObject[0].pointvalue * point;
return callback(i, totalprice);
}
});
}
};
You can use promises when you want to use the result of two functions and later use it for further computation.
In your case, you can execute the two asynchronous functions in parallel. It can look like this.
Promise.all([
asyncFunc1(),
asyncFunc2(),
])
.then(function(result){
// result is an array and has the response of the functions which is
// result[0] and result[1]
···
// you can manipulate the result of the functions here
})
.catch(function(err){
// Receives rejection/error among the Promises
···
});
Here asyncFunc1() will be your first find function
asyncFunc2() will be your second find function.
The result[0] and result[1] will be the results of the functions respectively.
Later you can use the result to do further computations.
Hope this helps.

nodejs issue with undefined promise nodejs using Q

I've been wrappingmind over async and sync functions as a php developer ive never had to worry about this much. so my issue is this
i have a function
function loadPartnerInventory(args,itemIds,offers,offer) {
var deferred = Q.defer();
offers.loadPartnerInventory(args, function(err, items) {
var checkItems = [];
var len = items.length;
for(var i = 0; i < itemIds.length; i++) {
for(var j = 0; j < items.length; j++) {
if (itemIds[i] == items[j].id){
//console.log('Pushed: ' + items[j].name);
checkItems.push({
itemname: items[j].market_name,
market_hash_name: items[j].market_hash_name,
steamid: offer.steamid_other,
tradeofferid : offer.tradeofferid
});
}
}
}
deferred.resolve(checkItems);
});
return deferred.promise;
}
function loadMyInventory(args,itemIds_g,offers,offer) {
var deferred = Q.defer();
offers.loadMyInventory(args, function(err, itemsg) {
var checkItems_g = [];
var len = itemsg.length;
for(var i = 0; i < itemIds_g.length; i++) {
for(var j = 0; j < itemsg.length; j++) {
if (itemIds_g[i] == itemsg[j].id){
console.log('Pushed: ' + itemsg[j].name);
checkItems_g.push({
itemname: itemsg[j].market_name,
market_hash_name: itemsg[j].market_hash_name,
steamid: offer.steamid_other,
tradeofferid : offer.tradeofferid
});
}
}
}
deferred.resolve(checkItems_g);
});
return deferred.promise;
}
function getPartnerInventory(offers, offer, itemIds, itemIds_g) {
var p1 = loadPartnerInventory({
partnerSteamId: offer.steamid_other,
appId: 730,
contextId: 2
},itemIds,offers,offer);
var p2 = loadMyInventory({appId: 730, contextId: 2},itemIds_g,offers,offer);
return Q.all(p1, p2).spread(function(checkItems, checkItems_g) {
return {
checkItems: checkItems,
checkItems2: checkItems_g
};
});
}
im doing this to get results, but somehow the second prommiss is undefined and i dont understand why.
getPartnerInventory(offers,offer,itemIds,itemIds_G).then(function(response) {
console.log(response);
//console.log(response);
});
checkitems returns correctly yet checkitems 2 is undefined.
cosole log is :
{ checkItems:
{ itemname: 'Operation Breakout Weapon Case',
market_hash_name: 'Operation Breakout Weapon Case',
steamid: '76561198245431424',
tradeofferid: '859881697' },
checkItems2: undefined }
Pushed: Glock-18 | Wraiths
as can see it its undiefined but seems to add item after its done
You cannot resolve the same promise twice. You should wrap both methods in separate promises and then use Q.all(), which will return a new promise to be resolved only after both promises have been successfully resolved. For example:
function mapOfferItem(item, offer) {
return {
itemname: item.market_name,
market_hash_name: item.market_hash_name,
steamid: args.offer.steamid_other,
tradeofferid : args.offer.tradeofferid
};
}
function loadPartnerInventory(args) {
var deferred = Q.defer();
offers.loadPartnerInventory(args, function(err, items) {
var checkedItems = items.filter(function(item) => {
return args.itemIds.indexOf(item.id) >= 0;
}).map(function(item) {
return mapOfferItem(item, args.offer);
});
deferred.resolve(checkedItems);
});
return deferred.promise;
}
function loadMyInventory(args) {
var deferred = Q.defer();
offers.loadMyInventory(args, function(err, items) {
var checkItems = items.filter(function(item) {
return args.itemIds_g.indexOf(item.id);
}).map(function(item) {
return mapOfferItem(item, args.offer);
});
deferred.resolve(checkItems);
});
return deferred.promise;
}
function getPartnerInventory(offers, offer, itemIds, itemIds_g) {
var p1 = loadPartnerInventory({
partnerSteamId: offer.steamid_other,
appId: 730,
contextId: 2
});
var p2 = loadMyInventory({appId: 730, contextId: 2});
return Q.all([p1, p2]).spread(function(checkItems, checkItems_g) {
return {
checkItems: checkItems,
checkItems2: checkItems_g
};
});
}
You can then use the function like this:
getParentInventory(offers, offer, itemIds, itemIds_g)
.then(function(checkItems) {
console.log(checkItems);
// should print {checkItems: [...], checkItems2: [...]}
});

Q Promise Nodejs how to resolve in loop

i have code written in nodejs make me confusying using Q Promises
theFunction()
.then(function(data) {
var deferred = Q.defer()
var result = [];
for(i=0; i < data.length; i++) {
secondFunc(data.item)
.then(function(data2) {
data.more = data2.item
});
result.push(data);
}
deferred.resolve(result);
deferred.promise();
});
i want data in second function inside loop can push into result
so my previous data is like this
[
{
id: 1,
item: 1,
hero: 2
},
{
id: 1,
item: 1,
hero: 2
}
]
and so like this
[
{
id: 1,
item: 1,
hero: 2,
more: {
list: 1
}
},
{
id: 1,
item: 1,
hero: 2,
more: {
list: 4
}
}
]
I've tried several ways start by entering the command
deferred.resolve (); statement in the loop and only showing 1 data
have any solution ?
Instead of a deferred.resolve() on an array which will resolve immediately, use Q.all which waits for an array of promises:
theFunction()
.then(function(data) {
var result = [];
for(var i=0; i < data.length; i++) (function(i){
result.push(secondFunc(data[i].item)
.then(function(data2) {
data[i].more = data2.item;
return data[i];
}));
})(i); // avoid the closure loop problem
return Q.all(result)
});
Or even better:
theFunction()
.then(function(data) {
return Q.all(data.map(function(item)
return secondFunc(item)
.then(function(data2) {
item.more = data2.item;
return item;
});
});
});
I know this is a older post but I've the same problem and did not found any solution. Maybe someone here find a good solution very fast.
function CompareTeamspeakClients(forumUsers) {
var promises = [];
var tsClient = new TeamSpeakClient("127.0.0.1", 10011);
tsClient.send("login", {
client_login_name: "serveradmin",
client_login_password: "M+h8YzUA"
}, function(err, response){
if (err) deferred.reject(err);
});
tsClient.send("use", {
port: 9987
}, function(err, response){
if (err) deferred.reject(err);
});
forumUsers.forEach(function(user, index){
var deferred = Q.defer();
tsClient.send("clientdbfind", ["uid"], {
pattern: user.tsid
}, function(err, response){
if (err) deferred.reject(err);
if (response) {
tsClient.send("clientdbinfo", {
cldbid: response.cldbid
}, function(err, response){
if (err) deferred.reject(err);
forumUsers[index]['tsdbid'] = response.client_database_id;
forumUsers[index]['tsnickname'] = response.client_nickname;
forumUsers[index]['tslastconnected'] = response.client_lastconnected;
deferred.resolve(forumUsers);
});
}
});
promises.push(deferred.promise);
});
console.log(promises);
return Q.all(promises);
}

Resources