Karan Kumar
Karan Kumar

Reputation: 3176

Node : Cannot pass mongodb id equality check

I know it sounds dumb but I really wonder what is it that I am missing here?

Error: It's not successfully checking for Mongodb's id equality rather throwing NotAuthorizedError.

Code:

router.post('/:id', currentUser, authenticate, async (
    req: Request, res: Response
) => {

    const id = req.params.id
    const project = await Project.findById(id)
    if (!project) throw new NotFoundError()

    console.log(`CURRENT USER "${req.currentUser!.id}" AND PROJECT OWNER "${project.user}"`)
// OUTPUT : 
//CURRENT USER "5f499321ecae75127075ebae" AND PROJECT OWNER "5f499321ecae75127075ebae"

    if (project.user !== req.currentUser!.id) throw new NotAuthorizedError()
    await project.remove()
    res.status(201).send({})
})

It is supposed to be equal as clearly logged in the console as well

Upvotes: 0

Views: 29

Answers (1)

36ve
36ve

Reputation: 633

My guess is that you are comparing a string with a wrapper implementing a custom toString, hence testing the string against a reference. Did you try comparing to project.user.toString() ?

Upvotes: 1

Related Questions