猫鼬-对指定文档进行更新时的调用功能

时间:2019-01-22 17:18:48

标签: node.js mongodb mongoose

我在文件A.js中有此功能

exports.executeMethods = async function(_id) {
    var customObject = new CustomObject(_id);
    await customObject.someAction();
}

插入新文档后,B.js会调用

exports.insertDocument = function(document) { 
    document.save(async function(err, document) { //let's say this document has _id = 1
        if(err) {
            //stuff
        }
        else {
            A.executeMethods();
        }
    });
}

当带有CustomObject的文档更新时,我需要一种在相同的_id = 1对象中执行另一种方法的方法。

我尝试过的一件事是变更流,我在B.js中写了另一个函数

exports.watch_document = async function(_id) {
    var changeStream = models.Task.watch(
        [{
            $match: { 
                _id: _id 
            }
        }]
    );
    return await changeStream.next();
}

然后我从executeMethods修改了A.js

exports.executeMethods = async function(_id) {
    var customObject = new CustomObject(_id);
    await customObject.someAction();
    var res = await B.watch_document(_id);
    if(//condition) {
         await customObject.someOtherAction();
    }
}

但是由于某种原因changeStream.next is not defined,而且我也不认为此解决方案真正有效,因为我不得不创建一个replica set来尝试这一方法。有没有更好的办法?如果没有,为什么没有定义?我有mongodb 4.0和最新版本的猫鼬

编辑:我忘了提到这是一个REST应用程序,并且在调用someOtherAction()之前所需的信息必须由外部用户提供。

0 个答案:

没有答案