带回调的Collection.update()返回undefined

时间:2016-04-24 12:15:23

标签: mongodb asynchronous meteor

在Meteor中,我使用客户端上Collection.update()(1)的返回值向用户显示消息。该方法在客户端和服务器上运行。

考虑以下方法,该方法按预期工作(返回1):

Meteor.methods({
    updateDocument(documentId) {
        return Collection.update(
            documentId, 
            {$set: {updatedBy: this.userId}}
        );
    }
});

接下来,请考虑以下方法,该方法返回undefined

Meteor.methods({
   updateDocument(documentId) {
        return Collection.update(
            documentId,
            {$set: {updatedBy: this.userId}},
            (error, result) => {
                return 1;
            }
        );
    }
});

我在这里缺少什么?为什么updateDocument()不返回1

1 个答案:

答案 0 :(得分:2)

当您提供回调函数时,update调用是异步的。它什么都不返回,你的Meteor方法可能会在更新完成之前完成。

引自Meteor docs

  

在服务器上,如果您不提供回调,则更新阻止   直到数据库确认写入,否则抛出异常   有些不对劲。如果确实提供了回调,则返回更新   立即。更新完成后,将使用a调用回调   失败时的单个错误参数,或第二个参数   如果更新,则指示受影响的文档的数量   成功的。