its me its me - 11 days ago 9
Node.js Question

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

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

        });
    }


};
Comments