简体   繁体   中英

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.

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?

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)
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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