[英]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]"
但我收到"Callback must be a function, got [object Object]"
的错误
I have tried to $set: update and $set: profileImage but still not working.我曾尝试 $set: update 和 $set: profileImage 但仍然无法正常工作。
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: 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.