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);
}
});
}
};
10
solved How can get two collection documents and calculate points using express.js?