簡體   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