如何在nodejs的foreach循环中编写顺序执行?

时间:2017-07-03 14:43:25

标签: node.js mongoose koa

我是nodejs的新手。请帮我如何在foreach循环中保存数据并将响应发送回controller.it返回false而不是返回对象。提前致谢

这是我的代码,如

var rideStatus = require('../models/ridestatus');
module.exports = {
  invite_ride: function* (body) {
    var saved = false;
    var receivers = body.uids;
    receivers = receivers.split(',').filter(function(n) { return n != 'null'; });
    receivers.forEach(function* (n) {
      yield rideStatus.findOne(
        {$and: [{ride_id: body.rideid}, {receiver_id: receivers}]},
        function(err, doc) {
          if (doc === null) {
            new rideStatus({
              ride_id: body.rideid,
              invited_id: body.userId,
              receiver_id: receivers,
            }).save(function(err1, ridestatus) {
              if (!err1) {
                rideStatus.findOne({_id: ridestatus._id}).
                  populate('ride_id').
                  populate('invited_id').
                  populate('receiver_id').
                  exec(function(err2, user) {
                    // console.log(user);
                    if (user != null) {
                      saved = user;

                    }
                  });
              }
            });
          }
        });
    });
    return saved;
  },
};

2 个答案:

答案 0 :(得分:1)

由于node.js的异步性质,你的“return save”语句不会等待你的i / o完成他的工作。并且还要注意,如果要保存多个值,则应返回已保存的Users对象数组。

var rideStatus = require('../models/ridestatus');
module.exports = {
    invite_ride: function* (body) {
        var savedUsers = [];
        var receivers = body.uids;
        receivers = receivers.split(',').filter(function(n) { return n != 'null'; });
        var len = receivers.length
        var i=0
        receivers.forEach(function* (n) {
            yield rideStatus.findOne(
                {$and: [{ride_id: body.rideid}, {receiver_id: receivers}]},
                function(err, doc) {
                    if (doc === null) {
                        new rideStatus({
                            ride_id: body.rideid,
                            invited_id: body.userId,
                            receiver_id: receivers,
                        }).save(function(err1, ridestatus) {
                            if (!err1) {
                                rideStatus.findOne({_id: ridestatus._id}).
                                    populate('ride_id').
                                    populate('invited_id').
                                    populate('receiver_id').
                                    exec(function(err2, user) {
                                        // console.log(user);
                                        if (user != null) {
                                            savedUsers.push(user);
                                        }
                                        if(i==len-1)
                                            return savedUsers;
                                        else
                                            i++
                                    });
                            }else{
                                i++
                            }
                        });
                    }else{
                        i++
                    }
            });
        });
    },
};

答案 1 :(得分:0)

查看链接上的图书馆:async