-4
router.post("/cart/paycash/add-order",(req,res) => {
    req.checkBody("emri","Ju lutem vendosni emrin").notEmpty();
    req.checkBody("mbiemri","Ju lutem vendosni mbiemrin").notEmpty();
    req.checkBody("numritelefonit","Ju lutem vendosni numrin e telefonit").notEmpty();
    req.checkBody("qyteti","Ju lutem vendosni qytetin").notEmpty();
    var emri = req.body.emri;
    var mbiemri = req.body.mbiemri;
    var telefoni = req.body.numritelefonit;
    var email = req.body.email;
    var qyteti = req.body.qyteti;
    var adresa = req.body.adresa;

    var Cart = req.session.cart;

    var errors = req.validationErrors();
    if(errors) {
        res.redirect("/cart/checkout", {
            errors:errors
        });
    }
    else {
        Orders.find({}, function(err,orders) {
            if(err) {
                console.log(err);
            }
            else {
                var order=new Orders({
                    emri:emri,
                    mbiemri:mbiemri,
                    telefoni:telefoni,
                    email:email,
                    qyteti:qyteti,
                    adresa:adresa,   
                });
                console.log(Cart.length);
                Cart.forEach(function(product) {
                    var cart = Cart.length;
                    var productTitle = product.title;
                    console.log(productTitle);
                    for (var i = 0; i < 1; i++) {
                       Products.findOne({title:product.title}, function(err,foundproduct) {
                           console.log(foundproduct.title)
                           order.products.push(foundproduct);   
                           order.save();;
                        });
                    }
                });
            }
        });

       delete req.session.cart;
        delete req.session.promocode;
        res.redirect("/dyqani");
    }
});

I want to make this code works, but for this I need async. I have tried some methods but I couldn't made it work. Can anyone help me? I want to be able to add in my order database all the products that are in the cart, but because mongoose is async and JavaScript is not, some of the queries get loaded before and the results in my database are not the ones that are in my cart.

pushkin
  • 9,575
  • 15
  • 51
  • 95

1 Answers1

0

It seems like you're trying to asynchronously request for every product that's in your cart. You don't need the nested for loop in your forEach function, because that loop doesn't seem to do anything.

You need to first map all the async requests in an array and run Promise.all to request them all asynchronously

let products = Cart.map((product) => {
    return Products.findOne({title:product.title},(err,foundproduct) => {
      //your save product to order logic
   });
})

Promise.all(products).then((complete) => {
    console.log('product added')
})

Here's a detailed explanation on how promises work in javascript Javascript Promises

Your application design pattern is also uncommon, but that's outside the scope of this question.

mxdi9i7
  • 677
  • 4
  • 13
  • This won't work if `Products.findOne` doesn't return a promise - which it looks like it doesn't since it has a callback function. – Flowers4Life Apr 30 '18 at 17:45
  • 1
    @Flowers4Life Mongoose works with both promises and callbacks; in the case of [findOne](http://mongoosejs.com/docs/api.html#query_Query-findOne), it returns a ]mongoose query object](http://mongoosejs.com/docs/queries.html) which has a `.then()` method allowing it to function as a promise. – Vince Bowdren May 01 '18 at 14:39
  • Completely missed Mongoose, thanks for the explanation! – Flowers4Life May 01 '18 at 16:34