简体   繁体   中英

Error in updating profile with image using mongoose and cloudinary

 updateProfile: async function(req, res) {
    try {
        const update = req.body;
        const id = req.params.id;

        if (!req.files || Object.keys(req.files).length === 0) {
            return res.status(400).send('No files were uploaded.');
        }


        const image = req.files.profileImage;

        const cloudFile = await upload(image.tempFilePath);
        const profileImage = cloudFile.url

        console.log('Loging cloudfile', profileImage)

        await User.updateOne(id, { update }, { profileImage }, { new: true },
            function(err, doc) {
                if (err) {
                    console.log(err)
                }
                if (doc) {
                    return res.status(200).send({ sucess: true, msg: 'Profile updated successful' })
                }
            });

    } catch (error) {
        res.status(500).json({ msg: error.message });
    }

}

But I'm getting an error of "Callback must be a function, got [object Object]"

I have tried to $set: update and $set: profileImage but still not working.

So the image successful upload into the cloudinary but the update for mongoose is not working.

Upon brief research into the issue, I think you are feeding the arguments in wrong. Objects can be confusing but not to worry.

Your code is:

await User.updateOne(id, { update }, { profileImage }, { new: true }

However, I believe it should be something more like:

await User.updateOne({id: id}, { profileImagine: profileImage, new: true },

The API reference annotates use of the function as:

const filter = { name: 'John Doe' };
const update = { age: 30 };

const oldDocument = await User.updateOne(filter, update);
oldDocument.n; // Number of documents matched
oldDocument.nModified; // Number of documents modified

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