Unable to delete a document in mongodb using DELETE method - node.js

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>)

Are you missing await
**Route Delete Method**
router.delete('/:id', async (req, res) => {
const article = await Article.findByIdAndDelete(req.params.id)
res.send(article)
})

Related

Mongoose typeError: findById is not a function

I'm trying to find an item by its ID, so I can pass it into a view with a pre-populated form so the user can update the item within the database.
The controller looks like this:
exports.item_update_get = (req, res) => {
Promise.all([
Item.findbyId(req.params.id).populate("category"),
Category.find()
]).then(([itemResults,categoryResults])=>{
res.render('itemForm',{categories:categoryResults,item:itemResults});
}).catch((err)=>{
console.log(err)
})
};
And the schema (if relevant):
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const ItemSchema = new Schema({
name: { type: String, required: true},
description: { type: String},
category:{ type: Schema.Types.ObjectId, ref: "Category"},
price: { type: String},
stock: { type: Number}
});
module.exports = mongoose.model("Item", ItemSchema);
However, I keep getting the error:
Item.findbyId is not a function
TypeError: Item.findbyId is not a function
Could someone help point out the mistake I'm making?
Thank you

MongoDB + Node : How to insert data to a collection that reuses a model with other collections?

I want to reuse my message model in 2 different (or more) collections to save duplicate data in the db. After doing some research I belief using refPath is the common way to do that (see my code). However, Im wondering how to use such schema. How do I add a notification (collection using message)? Do I need to insert a message to Message collection first? How will the Notification 'connect' to it?
const NotificationSchema = new mongoose.Schema({
to: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
message: [{
content: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Message'
}
}]
});
const NotificationSchema = new mongoose.Schema({
to: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
message: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Message'
}]
});
const messageSchema = new mongoose.Schema({
to_type: String,
to: {
type: mongoose.Schema.Types.ObjectId,
refPath: 'to_type'
},
title: String,
body: String,
}
...
});
Would I have to insert into both collections : Notification and Message collection?!
const addNotification = (req, res) => {
const {user} = req.user;
const {title, body} = req.body;
const notification = new Notification({user:user})
const message = new Message({title: title, body: body, to: notification._id})
const notification = new Notification({user:user, message: message})
}

Get the data inside request array inside of a schema

How can I create a route to get postedby data which is inside requests array. The request array is inside of a Post schema
const mongoose = require("mongoose")
const {ObjectId} = mongoose.Schema.Types
const postSchema = new mongoose.Schema({
name:{
type: String,
required: true
},
jobtitle:{
type: String,
required: true
},
requests:[{
text: String,
postedby:{type: ObjectId, ref: "User"}
}],
postedby:{
type: ObjectId,
ref: "User"
}
})
mongoose.model("Post", postSchema)
I tried this but I was getting another postedby
router.get("/acceptedpost",requireLogin,(req,res)=>{
Post.find({postedby:req.user._id})
.populate("postedby","_id name")
.then(accpost => {
res.json({accpost})
})
.catch(err => {
console.log(err)
})
})
If you want to match the req.user._id against the requests array, you can simply do:
Post.find({"requests.postedby": req.user._id})
.populate("postedby", "requests.postedby")
.then(accpost => {
res.json({accpost})
})
.catch(err => {
console.log(err)
})

Not able to get all document details from MongoDB whilst passing objectId through params

I am only getting the object which is present in the Document...
this is the line of code of the route
router.get('/product-by-id/:ProductId', async (req,res) => {
const {ProductId} = req.params;
const Id= await Prod.findById({_id:ProductId})
return res.send(Id);
})
const mongoose= require('mongoose');
const Schema= mongoose.Schema;
const productSchema = mongoose.Schema({
author: {
type: Schema.Types.ObjectId,
ref: 'users'
},
title: {
type:String,
maxlength: 50
},
description:{
type: String
},
file: {
data: Buffer,
contentType: String
},
duration:{
type:String
},
cat: {
type:String,
enum:["Shoes","Appliances","Apparel","Accessories","Electronics","Books","PC Parts"]
},
barter:{
type: String
}
}, {timestamp: true })
const Prod = mongoose.model('Prod', productSchema);
module.exports= Prod
I tested the route on Postman but only get the file object and not the others ...
Help is much appreciated.
your method looks good just a small fix is needed while using findbyId directly pass id. like this
let ProductId = req.params.ProductId
const Id= await Prod.findById(ProductId)

How to setup Mongoose Schema for population

I have a problem with my mongoose schemas. I was able to populate one document from another, but I am unable to create a similar connection between other document.
I've been looking at this for a long time but I just don't see whats wrong. It seems to be setup correctly, but comments do not populate. I am using mongoose 5.4.5.
blogSchema
const mongoose = require('mongoose')
const blogSchema = mongoose.Schema({
title: String,
author: String,
url: String,
likes: Number,
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
}
]
})
blogSchema.set('toJSON', {
transform: (document, returnedObject) => {
returnedObject.id = returnedObject._id.toString()
delete returnedObject._id
delete returnedObject.__v
}
})
const Blog = mongoose.model('Blog', blogSchema)
module.exports = Blog
commentSchema
const mongoose = require('mongoose')
const commentSchema = mongoose.Schema({
text: {
type: String,
minlength: 3,
present: true
},
blog: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Blog'
}
})
commentSchema.set('toJSON', {
transform: (document, returnedObject) => {
returnedObject.id = returnedObject._id.toString()
delete returnedObject._id
delete returnedObject.__v
}
})
const Comment = mongoose.model('Comment', commentSchema)
module.exports = Comment
userSchema
const mongoose = require('mongoose')
const uniqueValidator = require('mongoose-unique-validator')
const userSchema = mongoose.Schema({
username: {
type: String,
unique: true,
minlength: 3,
present: true
},
name: String,
passwordHash: String,
blogs: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Blog'
}
],
})
userSchema.plugin(uniqueValidator)
userSchema.set('toJSON', {
transform: (document, returnedObject) => {
returnedObject.id = returnedObject._id.toString()
delete returnedObject._id
delete returnedObject.__v
delete returnedObject.passwordHash
}
})
const User = mongoose.model('User', userSchema)
module.exports = User
populate
router.get('/', async (request, response) => {
const blogs = await Blog.find({})
.populate('comment', { text: 1 })
.populate('user', { username: 1, name: 1 })
response.json(blogs.map(b => b.toJSON()))
})
I am able to populate user correctly to blogSchema, but populating Comment doesnt work. The order of the populate calls do not change the situation and if I call populate only for comment it doesn't work anyway.
I suppose that there is a problem with my schemas, but I just am unable to see it.
Well...In your blog it's called comments but you try to populate comment. I think that's the issue.

Resources