Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 猫鼬序贯承诺_Javascript_Mongoose_Promise_Bluebird - Fatal编程技术网

Javascript 猫鼬序贯承诺

Javascript 猫鼬序贯承诺,javascript,mongoose,promise,bluebird,Javascript,Mongoose,Promise,Bluebird,我尝试按顺序执行一些动态查询,但由于任何原因,下一个代码都无法实现所需的行为 var createEvent = function (user, notification) { var action, query; query = { agent: notification.agent, story: notification.story, type: notification.type }; action = { agent: notification.agent, story:

我尝试按顺序执行一些动态查询,但由于任何原因,下一个代码都无法实现所需的行为

var createEvent = function (user, notification) {
  var action, query;

  query = { agent: notification.agent, story: notification.story, type: notification.type };
  action = { agent: notification.agent, story: notification.story, type: notification.type, ts: notification.ts };

  return mongoose.model('Event').findOne(query).exec()
    .then(function (response) {
      if (response === null) {
        return mongoose.model('Event').create(action)
          .then(function (response) {
            return mongoose.model('User').findByIdAndUpdate(user, { $push: { notifications: { _id: response._id }}});
          });
      }
      return mongoose.model('User').findByIdAndUpdate(user, { $push: { notifications: { _id: notification._id }}}).exec();
    });

  setTimeout(resolve, 3000);
};

var moveNotifications = function (users) {
  var promises = [];

  users.map(function (user) {
    if (user.notifications.length > 0) {
      user.notifications.map(function (notification) {
        promises.push(createEvent(user._id, notification));
      });
    }
  });

  Promise.each(promises, function (queue_item) {
    return queue_item();
  });
};

有人能帮我吗?

当你在嵌套的
数组#map
循环中调用
createEvent
时,你正在一次启动所有的查询-你要做的就是得到一个
id
通知的数组,以便稍后传递到
Promsise中的
createEvent
。每个

var moveNotifications = function(users) {
    var items = [];
    users.forEach(function(user) {
        if (user.notifications.length > 0) {
            user.notifications.forEach(function(notification) {
                items.push({id: user._id, notification: notification});
            });
        }
    });
    return Promise.each(events, function(item) {
        return createEvent(item._id, item.notification);
    });
}
注意:不确定为什么要使用
Array#map
,因为您从不从map回调返回任何内容-您基本上是在执行
Array#forEach

var moveNotifications = function(users) {
    var items = [];
    users.forEach(function(user) {
        if (user.notifications.length > 0) {
            user.notifications.forEach(function(notification) {
                items.push({id: user._id, notification: notification});
            });
        }
    });
    return Promise.each(events, function(item) {
        return createEvent(item._id, item.notification);
    });
}
或者,使用
Array#concat
将通过正确使用(嵌套的)
Array#map
返回的两级数组展平,可以获得相同的结果

var moveNotifications = function(users) {
    return Promise.each([].concat.apply([], users.map(function(user) {
        return user.notifications.map(function(notification) {
            return {id: user._id, notification: notification};
        });
    })), function(item) {
        return createEvent(item._id, item.notification);
    });
}
使用以下ES2015语法,上述内容更为简洁:

  • 箭头函数
    =>
  • 排列运算符
  • 速记对象属性名称
    {a,b,c}
  • 解构赋值-参数上下文匹配
    ({a,b,c})=>

极限ES2016单行版本:p

var moveNotifications = users => Promise.each([].concat(...users.map(user => user.notifications.map(notification => ({id: user._id, notification})))), ({id, notification}) => createEvent(id, notification));

有几件事<代码>设置超时(解析,3000)永远不会执行,因为它是在
返回之后执行的
——但同样的,
resolve
也没有定义。其次,您在.map回调中调用
createEvent
,因此所有这些
findOne
调用在第一次调用之前都是“正在进行中”。然后可以调用我不理解创建迭代承诺的概念!因此,感谢您的回复,我发现它更加清晰!谢谢