[英]How to wait for findOneAndUpdate to finish before continuing async.series
I've got a seed file setup to populate my mongodb. 我有一个种子文件设置来填充我的mongodb。 I am setting up a counter so I can automatically increment a value when I add a new recipe to my db.
我正在设置一个计数器,以便在向数据库中添加新配方时可以自动增加一个值。 Right now I'm using async.series to run the different tasks in the seed file.
现在,我正在使用async.series来运行种子文件中的不同任务。 In what I've included you can see where I set recipe_id equal to this methods return value getNextSequence(counters, "recipeid")
在我所包含的内容中,您可以看到我在哪里设置了method_id等于该方法的返回值getNextSequence(counters,“ recipeid”)
getNextSequence uses the Counter model to query (findOneAndUpdate) the db. getNextSequence使用Counter模型来查询(findOneAndUpdate)数据库。 I believe the callback for findOneAndUpdate is never called though because the callback for async.series is invoked first.
我相信尽管从未调用findOneAndUpdate的回调,因为首先调用了async.series的回调。 It's a funny problem though because I need to ensure that getNextSequence has returned successfully before moving on.
但是,这是一个有趣的问题,因为我需要确保getNextSequence在继续之前已成功返回。 Not sure the best way to go about this.
不确定执行此操作的最佳方法。
//seed.js //incomplete //seed.js //不完整
var getNextSequence = require('./utils/getNextSequence').getNextSequence;
recipes = new mongo.Collection(db, "recipes");
counters = new mongo.Collection(db, "counters");
async.series([
function(callback){
//initiate counter
counters.insert(
{
_id: "recipeid",
seq: 0
},function(err, rec){
callback(err, rec);
})
},
function(callback){
//add test recipes
recipes.insert([{
recipe_id: getNextSequence(counters, "recipeid"),
title:'title 1',
body:'body 1',
author: [seedUsers[1]._id],
categories: [ seedCategories[0]._id, seedCategories[1]._id ],
create_at: new Date()
},{
recipe_id: getNextSequence(counters, "recipeid"),
title:'title 2',
body:'body 2',
author: [seedUsers[1]._id],
categories: [ seedCategories[1]._id ],
create_at: new Date()
}], function(err, rec){
callback(err, rec);
})
}
],
// optional callback
function(err, results){
//console.log('\n', results);
db.close();
});
//getNextSequence.js //getNextSequence.js
var Counter = require('../counter').Counter;
exports.getNextSequence = function(counters, name) {
Counter.findOne({ '_id': 'recipeid' }, function (err, counter) {
//if (err) return handleError(err);
//never called
console.log('---counter seq=', counter.seq);
})
Counter.findOneAndUpdate({ _id: name }, { $inc: { seq: 1 } }, { new: true }, function(err, counter){
//never called
console.log('counter.seq', counter.seq);
return counter.seq;
}
)
} }
I'm not sure which async library you're using, let's assume it is this https://github.com/caolan/async 我不确定您使用的是哪个异步库,让我们假设它是这个https://github.com/caolan/async
You may want to run the getNextSequence
first, before inserting the receipts. 在插入收据之前,您可能需要先运行
getNextSequence
。 Changes the getNextSequence a little bit, so that the async
know when the findOneAndUpdate
process is done. 稍微更改getNextSequence,以便
async
知道findOneAndUpdate
进程何时完成。 I also changed to use waterfall
, so the result from one task is passed to the next 我还更改为使用
waterfall
,因此一个任务的结果传递给了下一个任务
var Counter = require('../counter').Counter;
exports.getNextSequence = function (counters, name, callback) {
Counter.findOne({'_id': 'recipeid'}, function (err, counter) {
//if (err) return handleError(err);
//never called
console.log('---counter seq=', counter.seq);
})
Counter.findOneAndUpdate({_id: name}, {$inc: {seq: 1}}, {new: true}, function (err, counter) {
//never called
console.log('counter.seq', counter.seq);
callback(null, counter.seq);
}
)
}
seeding 播种
var getNextSequence = require('./utils/getNextSequence').getNextSequence;
recipes = new mongo.Collection(db, "recipes");
counters = new mongo.Collection(db, "counters");
async.waterfall([
function (callback) {
//initiate counter
counters.insert(
{
_id: "recipeid",
seq: 0
}, function (err, rec) {
callback(err, rec);
})
},
function (callback) {
getNextSequence(counters, "recipeid", callback);
},
function (seq, callback) {
//add test recipes
recipes.insert({
recipe_id: seq,
title: 'title 1',
body: 'body 1',
author: [seedUsers[1]._id],
categories: [seedCategories[0]._id, seedCategories[1]._id],
create_at: new Date()
}, function (err, rec) {
callback(err, rec);
})
},
function (callback) {
getNextSequence(counters, "recipeid", callback);
},
function (seq, callback) {
//add test recipes
recipes.insert({
recipe_id: seq,
title: 'title 2',
body: 'body 2',
author: [seedUsers[1]._id],
categories: [seedCategories[1]._id],
create_at: new Date()
}, function (err, rec) {
callback(err, rec);
})
}
],
// optional callback
function (err, results) {
//console.log('\n', results);
db.close();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.