FOR循环之后的IF语句在mongoose查询完成之前执行

时间:2017-08-23 16:03:48

标签: node.js express mongoose

我希望FOR循环后的IF语句应该执行,一旦运行了FOR循环中的mongoose find查询。我遇到问题因为nodejs是异步的。任何人都可以使下面的代码同步,以便在将流程移动到IF语句之前执行包含mongoose查询的FOR循环

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

        });
    }
}

});

1 个答案:

答案 0 :(得分:-1)

你的for循环内部是Mongoose findOne。那是一个异步调用。因此for循环将完成并在异步调用完成之前移至If。

你应该使用类似async.each的东西进行Mongoose调用,然后在回调中使用If调用。