【发布时间】:2015-04-10 21:21:36
【问题描述】:
我正在尝试以下查询。我正在写这个来获取文档列表,对于每个文档我需要运行一些逻辑来获取标签列表。使用这个标签列表,我用获得的标签列表更新文档。
我将“updateAndPrint”功能的标签列表设为空白。我想这是一个与承诺出现的问题。
这里是查询:
DB.todoTable.find()
.limit(10)
.exec(function (err, todos) {
var tags = [];
for (var todo in todos) {
var text = todos[todo].text;
var id = todos[todo]._id;
console.log(text);
tags = getTagsList(text);
(function updateAndPrint(id, tags) {
DB.todoTable.update({_id: id}, {$addToSet: {tags: {$each: tags}}},
function (err, numberUpdated, result) {
if (err) throw err;
(function printResult(id) {
DB.todoTable.findOne({_id: id})
.exec(function (err, todo) {
if (err) throw err;
console.dir(todo.tags);
});
})(id);
});
})(id, tags);
}
console.dir(tags);
});
我怎样才能让这个查询运行。或者有没有更好的方法来做同样的逻辑。
编辑
'tags = getTagsList(text)' 必须在我运行更新操作之前执行。
【问题讨论】:
标签: javascript node.js mongodb mongoose promise