Node.js mongo/node异步检查数据库中是否存在文档,如果不存在,则修改对象

Node.js mongo/node异步检查数据库中是否存在文档,如果不存在,则修改对象,node.js,mongodb,coffeescript,Node.js,Mongodb,Coffeescript,我试图确定集合中是否存在文档(作为对象数组的一部分接收)。如果它不存在,我希望插入文档并向对象添加“unread=true” 上述内容的coffee脚本代码如下: updateEvents = (db, events, done) -> async.each events, (eventobj, cb) -> # check whether event exists in event collection db.Event.count eventobj, (err,

我试图确定集合中是否存在文档(作为对象数组的一部分接收)。如果它不存在,我希望插入文档并向对象添加“unread=true”

上述内容的coffee脚本代码如下:

updateEvents = (db, events, done) ->

async.each events, (eventobj, cb) ->
    # check whether event exists in event collection
    db.Event.count eventobj, (err, count) ->
        # if it doesn't
        if count is 0
            # insert the document and when finished set an unread property on this object to true
            db.Event.insert eventobj, (err) ->
                eventobj.unread = true
        # if it does, log that is was found
        else
            console.log "yep, found it"

# pass results back up chain to client
done null, events
分配后,当控制台记录时,未读属性出现在eventobj上,但是,当传递给客户端时,它不会持久存在。好像任务从未发生过。为什么会发生这种情况


这与类型错误问题不同,因为它涉及数据流问题,而不是任何类型的错误。

在执行
插入操作之前,应将
未读
设置为

eventobj.unread = true
db.Event.insert eventobj, (err) ->
    // error handling

而不是在回调函数中设置
未读
。当您在回调函数中设置它时,
eventobj
已经保存到数据库中,只需更改
eventobj
本身。

正如zangw所说,如果要在数据库中保存未读属性,您应该在insert之前设置属性。我从您的问题中得到的是,您只想返回事件数组,并将未读属性附加到数据库中不存在的每个对象。无论哪种情况,都应该更新实际事件数组以返回。 而不是:

eventobj.unread = true
做:


解决了。问题在于调用已完成回调时,async.each未完成执行。我将done回调放入一个匿名函数中,并将其作为可选回调追加到async.each函数中

updateEvents = (db, events, done) ->

async.each events, ((eventobj, cb) ->
    # check each event to see if it exists in users collection
    db.Event.count eventobj, (err, count) ->
        # if it doesn't
        if count is 0
            # # insert the document and when finished set an unread property on this object to true
            db.Event.insert eventobj
            eventobj.unread = true
            cb()
        # if it does, log that is was found
        else
            console.log "yep, found it"
            cb()
# pass results back up chain to client
), finished = -> done null, events

关于类似问题的不同问题的可能重复。这与错误无关,但与回调的工作方式有关。请原谅,没有咖啡脚本。。我从不使用它:)我希望将事件按原样插入数据库,并将未读属性附加到数据库中找不到的事件数组中的每个事件。基本上,接收事件,对照db记录检查每个事件,如果不在db中,则将其放入,用未读标记它,并向上传递链。好的,如果您按照我的建议添加代码,您应该能够向上传递事件数组(具有各自的未读属性)。不幸的是,它不起作用。更准确地说,尽管集合为空,但仍有约5%的事件被标记。当集合为空时,应标记%100个事件。似乎大多数事件上都没有赋值,为什么我只能猜测(毫无疑问是异步的)。事件对象上有唯一的id吗?这样做可能会产生奇怪的结果,事件数组中有重复的条目。不幸的是,这也不行。我可以让它不稳定地工作,我认为async.series在评估所有作业之前是不会完成的。@Nick,也许你是对的。。但是
async.series
?我只是在代码中找到了
async。
updateEvents = (db, events, done) ->

async.each events, ((eventobj, cb) ->
    # check each event to see if it exists in users collection
    db.Event.count eventobj, (err, count) ->
        # if it doesn't
        if count is 0
            # # insert the document and when finished set an unread property on this object to true
            db.Event.insert eventobj
            eventobj.unread = true
            cb()
        # if it does, log that is was found
        else
            console.log "yep, found it"
            cb()
# pass results back up chain to client
), finished = -> done null, events