简体   繁体   English

nodejs异步瀑布在第二个函数中使用猫鼬findOneAndUpdate

[英]nodejs async waterfall use mongoose findOneAndUpdate in the second function

I want to user async waterfall to upload a file and then return the remote file path and pass it to the second function which is mongoose findOneAndUpdate to save the path to user's document. 我想让用户异步瀑布上传文件,然后返回远程文件路径,并将其传递给第二个函数,即findOneAndUpdate以将路径保存到用户文档中。

async.waterfall({
    uploadedFile: function (acb) { fileUpload(req, res, acb); },
    user: function (acb) { 
        Users.findOneAndUpdate({ uuid: req.user.uuid }, 
            { $set: {
                file: 'RESULTFROMWATERFALL',
                email: req.body.email,
                name: req.body.name,
    } }).exec(acb); }
}, function (err, data) {
    if (err) {
        console.log(err);
    }
    console.log('result: ', data)
});

I am stuck with this and don't know how to proceed to get the result from function one and pass it to mongoose find and update function. 我对此一无所知,不知道如何继续从功能一获得结果并将其传递给猫鼬查找和更新功能。

If the upload function is used isolated, it works like this: 如果单独使用上载功能,则它的工作方式如下:

fileUpload(req, res, function(err, result) {
        console.log('i can see result here: ', result) // https//some.remote.path/file.txt
    })

could somebody help me fix this async.waterfall example? 有人可以帮我解决这个async.waterfall示例吗?

Try this:- 尝试这个:-

async.waterfall([
(callback) => {
    fileUpload(req, res, (err,data)=>{
        callback(err, data);
    });
},
(updateURL, callback)=>{
    Users.findOneAndUpdate({ uuid: req.user.uuid },
        { $set: {
                file: 'RESULTFROMWATERFALL',
                email: req.body.email,
                name: req.body.name,
            } }).exec(callback); 
}
}], (err, data) => {
if (err) {
    console.log(err);
}
console.log('result: ', data)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM