I need help with my backend. I'm using postman and here's the model of my back end:
name: {
type: String,
required: [true, 'Department Name is Required']
},
description: {
type: String,
required: [true, 'Description is Required']
},
agents: [
{
agentId: {
type: String,
required: [true, 'Agent ID is Required']
},
createdOn: {
type: String,
default: new Date()
}
}
]
What I'm trying to do is push documents in agents array but I'm getting some errors.
The routes and controllers are as follows:
Routes:
router.post('/enroll', (req, res) => {
UserController.enroll(req.body).then(result => res.send(result))
});
Controllers:
module.exports.enroll = (params) => {
return Department.findById({departmentId: params.departmentId}).then(department => {
department.agents.push({userId: params.userId})
return department.save().then((department, error) => {
return (err) ? false : true
})
})
}
This is the error that I'm getting: (node:9916) UnhandledPromiseRejectionWarning: CastError: Cast to ObjectId failed for value "{ departmentId: '60e27549c36af1272812c4e3' }" (type Object) at path "_id" for model "Department"
The target is look for the department id and will push the agent id that I already acquire.