![](/img/trans.png)
[英]NodeJS Express - TypeError: Converting circular structure to JSON
[英]Nodejs express Converting circular structure to JSON
我有数组中的图像,我正在更新数据库。 但是发出它显示此错误
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'MongoClient'
这是我的代码
const updateCompany = async (req, res) => {
const file = req.files;
if (!file) return res.status(400).send('No image in the request');
const companyLogoName = file[0].filename;
const companyImageName = file[1].filename;
const basePath = `${req.protocol}://${req.get('host')}/public/uploads/`;
// console.log(`${basePath}${companyLogoName}`);
// console.log(`${basePath}${companyImageName}`);
try {
const updateCompany = Company.findByIdAndUpdate(req.params.id, {
companyName: req.body.companyName,
companyDescription: req.body.companyDescription,
companyWebsite: req.body.companyWebsite,
companyDHeadQuater: req.body.companyDHeadQuater,
companySpecialities: req.body.companySpecialities,
companyFounded: req.body.companyFounded,
companyIndustry: req.body.companyIndustry,
companyMinEmploye: req.body.companyMinEmploye,
companyMaxEmploye: req.body.companyMaxEmploye,
companyType: req.body.companyType,
companyLogo: `${basePath}${companyLogoName}`,
companyImage: `${basePath}${companyImageName}`,
}, {
new: true
});
// console.log(updateCompany);
return res.status(200).json({ success: true, data: updateCompany })
} catch (err) {
console.log(err);
if (err.name === 'ValidationError') {
console.error(Object.values(err.errors).map(val => val.message))
return res.status(400).json({ success: false, message: Object.values(err.errors).map(val => val.message)[0] })
}
return res.status(400).json({ success: false, message: err })
}
};
在控制台中它显示正确的图像路径意味着 multer 文件等正在工作但问题正在更新
您没有将任何回调 function 传递给findByIdAndUpdate
,您也没有使用await
。 根据 mongoose 文档,如果没有传递回调, findByIdAndUpdate
将返回Query
object 的实例。 所以updateCompany
实际上是Query
object 的一个实例,它试图呈现为 json,这就是此行出现错误的原因:
return res.status(200).json({ success: true, data: updateCompany })
您可以使用await
或添加文档中指定的回调function 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.