我是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;
  },
};

最佳答案

由于node.js的异步特性,您的“返回保存”语句不会等待您的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++
                    }
            });
        });
    },
};

关于node.js - 如何在nodejs的foreach循环中编写顺序执行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44888453/

10-11 12:11