-1

I want the IF statement after FOR loop should get executed, once the mongoose find query inside FOR loop is run. I run into problems since nodejs is asynchronous. can anyone make the below code synchronous so that the FOR loop including mongoose query is executed before moving the flow to the IF statement

app.post('/success', function (req, res) {

var seatLength = req.body.seatNo;
var ageList = req.body.age;
var nameList = req.body.name;
var emailData = req.body.email;
var dataG = req.body.dateOfJourney;
var emailArray = [];
var bookedAlready = false;
var bookedSeats = [];
for (var i = 0; i < req.body.seatNo.length; i++) {

    var pname = req.body.name[i];
    var page = req.body.age[i]
    busModel.findOne({$and: [{"ticket.seatNumber": req.body.seatNo[i]}, {"dateOfJourney": dataG}, {"ticket.reserved": false}]}, function (err, doc) {


        if (doc) {
            doc.ticket[0].reserved = true;
            doc.ticket[0].passengerName = pname;
            doc.ticket[0].passengerAge = page;
            doc.ticket[0].passengerEmail = req.body.email;
            doc.ticket[0].passengerContact = req.body.contact;


            doc.save(function (err) {
                if (err) {
                    console.log('error');

                }
                else {
                    console.log("no error");
                    console.log(req.body.seatNo[i]);
                    bookedSeats.push(req.body.seatNo[i]);

                }
            })

        }
        else {
            console.log('error');
        }


    });

}

if (bookedSeats.length == seatLength.length) {
    emailArray.push(i);
    ejs.renderFile(__dirname + "/views/test.ejs", {
        seat: seatLength,
        name: nameList,
        age: ageList
    }, function (err, data) {
        if (err) {
            console.log(err);
        }
        else {

            let HelperOptions = {
                from: 'padinkit@gmail.com',
                to: emailData,
                subject: 'Hello World !',
                html: data
            };
            transporter.sendMail(HelperOptions, function (error, info) {
                if (error) {
                    console.log(error);
                }
                //console.log('mail sent');
                console.log(info);
            });

        }

    });
    res.send("hi")


}
if (bookedSeats.length != seatLength.length) {
    for (i = 0; i < bookedSeats.length; i++) {
        busModel.findOne({$and: [{"ticket.seatNumber": req.body.seatNo[i]}, {"dateOfJourney": dataG}, {"ticket.reserved": true}]}, function (err, doc) {
            if (doc) {
                doc.ticket[0].reserved = false;
                doc.ticket[0].passengerName = null;
                doc.ticket[0].passengerAge = null;
                doc.ticket[0].passengerEmail = null;
                doc.ticket[0].passengerContact = null;


                doc.save(function (err) {
                    if (err) {
                        console.log('error');

                    }
                    else {
                        console.log("seats which were reserved during exception" + req.body.seatNo[i]);

                    }
                })

            }
            else {
                console.log('error');
            }

        });
    }
}

});

Sriram J
  • 174
  • 1
  • 1
  • 9
  • That's good, that means javascript is working properly. – Kevin B Aug 23 '17 at 16:20
  • Possible duplicate of [Node mongoose find query in loop not working](https://stackoverflow.com/questions/21829789/node-mongoose-find-query-in-loop-not-working) – Kevin B Aug 23 '17 at 16:22

1 Answers1

-1

Inside your for-loop is the Mongoose findOne. That is an asynchronous call. So the for loop will finish and move onto the If before the asynchronous call is finished.

You should be using something like async.each for the Mongoose call and then have the If call in the callback.

Mark S.
  • 3,849
  • 4
  • 20
  • 22