Bonnard
Bonnard

Reputation: 389

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?

Upvotes: 0

Views: 440

Answers (1)

radhey shyam
radhey shyam

Reputation: 769

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

Upvotes: 3

Related Questions