我正在创建一个图书项目,其中我将图书图像保存到 cloudinary 中,并将 url 保存到 mongodb 数据库中,该数据库运行良好。但是当我更新我的书时,我在更新一本书时遇到了问题书的网址未更新,控制台给我错误无法读取未定义的属性(读取“地图”),我想用新的图像网址更新网址,但它不起作用请任何人都可以解决此问题
这是我的 update.js 代码
module.exports.updateBook = async (req, res) => {
try {
const { id } = req.params;
const book = req.body;
const singleBook = await Book.findById(id);
// Delete Prvious Url From the Cloudinary and Reset It to the new ..
cloudinary.v2.uploader.destroy(singleBook.image[0].filename);
book.image = req.files.map((f) => ({
url: f.path,
filename: f.filename,
}));
console.log("Single Book ===", singleBook);
const updateBook = await Book.findByIdAndUpdate(
id,
{ $set: book },
{ new: true }
);
if (updateBook) …Run Code Online (Sandbox Code Playgroud)