0

I am new to node and mongodb, I have created a embedded/nested document, when I am trying to delete it using router.delete method its resulting in the following error: TypeError: Converting circular structure to JSON at JSON.stringify (). How to fix this and delete my document?

I tried with both findByIdAndRemove and findByIdAndDelete of the mongodb method.

**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: Converting circular structure to JSON at JSON.stringify (<anonymous>)

1 Answer 1

3

Are you missing await

**Route Delete Method**
router.delete('/:id', async (req, res) => {
    const article = await Article.findByIdAndDelete(req.params.id)
    res.send(article)
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.