Here I want to show the todos created by the the user which is in params and also want to show the id of user in each todos array object.How Can I achieve this
Here is my userSchema
const mongoose = require('mongoose')
const userSchema = new mongoose.Schema({
name :{
type: String,
required: true
},
email:{
type: String,
required: true,
unique: true
},
phone:{
type: String,
required: true,
},
todos: [{
type: mongoose.Schema.ObjectId,
ref: 'Todo',
},
]})
userSchema.pre(/^find/ , function(next)
this.populate('todos')
next()
})
const User = mongoose.model('User' , userSchema)
module.exports =User
This is the getUserById code
const user= await User.findById(req.params.id).populate('todos')
if(!user){
return next(new AppError(`Cant find this user from that ID ${req.originalUrl} :(` ,404))
}
res.status(200).json({
status: 'sucess',
data:{
user
}
})
})
Thanks in advance :)
