2

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.

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107

2 Answers2

2

Try

Change

return Department.findById({departmentId: params.departmentId}).

to

return Department.findById(params.departmentId)


findById accept only id not Object

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
  • Thanks. Could you please tell me how to post it in postman? I mean, how do I test it there? What I'm using is this: { "userId": "60e17238415fab21f8cc6b28", "departmentId": "60e27549c36af1272812c4e3" } – Mark Willow Aldave Jul 05 '21 at 08:32
  • @MarkWillowAldave Please share your route details ? Also check - https://stackoverflow.com/questions/17007997 – Tushar Gupta - curioustushar Jul 05 '21 at 08:37
  • Not sure if this is correct: ```router.post('/enroll', (req, res) => { UserController.enroll(req.body).then(result => res.send(result)) });``` – Mark Willow Aldave Jul 05 '21 at 08:40
  • 1
    Tushar can you help me with this thread :-) https://stackoverflow.com/questions/68254282/javascript-mongodb-usestate-problem-null-values-on-initial-registration – Mark Willow Aldave Jul 05 '21 at 10:14
1

Thanks Tushar! You're very helpful.

I was able to find the answer using the link you've provided and realized that I'm making some mistakes with the code itself. Hahahaha

I've modified routes to:
router.post('/enroll/:id', (req, res) => {
    const params = {
        departmentId: req.params.id,
       agentId: req.body.agentId
    }
    
    UserController.enroll(params).then(department => res.send(department))
});
Then, some of the lines in my controllers too:
module.exports.enroll = (params) => {
    return Department.findById(params.departmentId).then(department => {
        department.agents.push({agentId: params.agentId})

        return department.save().then((department, error) => {
            return (error) ? false : true
        })
    })
}

It is now working.