Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

node.js - 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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);
            }

        });
    }


};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...