繁体   English   中英

使用 mongoose 和 cloudinary 使用图像更新配置文件时出错

[英]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 });
    }

}

但我收到"Callback must be a function, got [object Object]"的错误

我曾尝试 $set: update 和 $set: profileImage 但仍然无法正常工作。

所以图像成功上传到云端,但猫鼬的更新不起作用。

在对该问题进行简要研究后,我认为您的论点是错误的。 对象可能会令人困惑,但不要担心。

你的代码是:

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

但是,我认为它应该更像是:

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

API 参考将函数的使用注释为:

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

暂无
暂无

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

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