Ich versuche, einige dynamische Abfragen sequentiell zu tun, aber aus irgendeinem Grund, erfüllt der nächste Code dieses gewünschte Verhalten nicht.mongoose sequentielle Versprechen
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();
});
};
Könnte mir jemand helfen?
paar Dinge ... 'setTimeout (resolve, 3000);' wird nie ausgeführt, weil es nach einem 'return' ist - aber das ist genauso gut,' resolve' ist sowieso nicht definiert. Zweitens rufen Sie 'createEvent' in einem .map-Callback auf - also sind alle diese' findOne' Aufrufe "im Flug" vor dem ersten .Dann kann –