Meteor.wrapAsync似乎不适用于db集合插入
问题描述:
我有以下异步Meteor方法。但createItem()
未返回前等待Items.insert
的回拨。Meteor.wrapAsync似乎不适用于db集合插入
# Relevant pieces shown here (coffeescript, more parens than usual for js folks :-)
Meteor.methods(
createItem : (doc, callback)->
asyncInsert = Meteor.wrapAsync(Items.insert, Items)
results = asyncInsert(doc, (err, result) ->
return callback(err, result)
)
return results
)
asyncMeteorCall = Meteor.wrapAsync(Meteor.call)
status = asyncMeteorCall("createItem", {name:"some item"}, (err, result)->
if err?
return "Error adding Item"
else
return "Successfully added Item"
)
console.log(status) # prints : undefined
我没有找到这个possibly related issue,但不知道这是否是相关的,我实际上做上述所有的服务器上。
答
接受回调的所有Mongo.Collection方法在服务器上调用时会自动同步运行(只要省略了回调),所以不需要包装它们。您应该可以直接致电Items.insert()
。
“在服务器上,如果你不提供一个回调,然后插入块,直到该数据库确认写,或是否有出错抛出异常。”
明白了,我已经读过,但并不理解添加回调会阻止阻塞效果。谢谢! – tolmark 2015-02-25 00:26:24