MongoDB - Cast to ObjectId failed for value “create” at path “_id”

前端 未结 2 1002
梦毁少年i
梦毁少年i 2021-01-20 21:40

No matter what I try, I can\'t get rid of this error and I have several other features in my application that create instances of Mongoose Models which look almost exactly l

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-20 22:11

    I should have posted more of my code to help diagnosis this issue. It turns out my /project/create route was the issue.

    I also had another route to view a project that was /project/:id. I supposed the /create part of my route was interfering with this logic so I changed the paths in my routes and all is working just fine now.

    // Before
    router.get('/projects', projects); // view all projects
    router.get('/project/:id, project); // view a project
    router.post('/project/:id', projectUpdate); // update a project
    router.post('/project/create', projectCreate); // create a project
    router.post('/project/delete/:id', projectDelete); // delete a project
    

    My routes for this feature of my app now look like this:

    // After
    router.get('/projects', projects); // view all projects
    router.get('/project/:id', project); // view a project
    router.post('/project/', projectCreate); // create a project
    router.post('/project/:id', projectUpdate); // update a project
    router.post('/project/delete/:id', projectDelete); // delete a project
    

    I learned a lesson today! Thanks for the help @Hypermattt.

提交回复
热议问题