-2
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);
its me
  • 524
  • 6
  • 28
  • 62

2 Answers2

1

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.

bhapri
  • 119
  • 1
  • 10
  • http://stackoverflow.com/questions/41124705/based-on-value-how-to-create-new-document-using-node-js-mongodb – its me Dec 14 '16 at 07:07
1
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);
            }

        });
    }


};
Beginner
  • 4,118
  • 3
  • 17
  • 26