我是node和mongodb的新手,当我尝试使用router.delete方法删除它时,创建了一个嵌入式/嵌套文档,它导致以下错误:TypeError:将圆形结构转换为JSON.stringify的JSON ()。如何解决此问题并删除我的文档?
我尝试了mongodb方法的findByIdAndRemove和findByIdAndDelete。
**Article Schema**
const articleSchema = new mongoose.Schema({
articleName: {
type: String
},
author: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Author'
}],
comments: [commentSchema]
})
const Article = mongoose.model('Article', articleSchema)
**Route Delete Method**
router.delete('/:id', async (req, res) => {
const article = Article.findByIdAndDelete(req.params.id)
res.send(article)
})
**Comment Schema**
const commentSchema = new mongoose.Schema({
articles: {
type: new mongoose.Schema({
articleName: {
type: String
},
author: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Author'
}],
})
},
users: {
type: new mongoose.Schema({
name: String
})
},
comment: String
})
**User Schema**
const userSchema = new mongoose.Schema({name: String, email: String})
UnhandledPromiseRejectionWarning:TypeError:转换循环structure to JSON at JSON.stringify (<anonymous>)
答案 0 :(得分:1)
您是否错过了await
**Route Delete Method**
router.delete('/:id', async (req, res) => {
const article = await Article.findByIdAndDelete(req.params.id)
res.send(article)
})