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

前端 未结 2 1000
梦毁少年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.

    0 讨论(0)
  • 2021-01-20 22:15

    My best guess is that there is an error in some other part of your application, as the code pasted into the question seems to work flawlessly.

    Here is some sample code I wrote in trying to debug your error. Try running it and see if it works on your local.

    index.js:

    'use strict';
    
    const mongoose = require( 'mongoose' );
    mongoose.Promise = Promise;
    mongoose.connect( 'mongo' );
    
    const ProjectSchema = new mongoose.Schema( {
      title: String,
      description: String
    } );
    
    const Project = mongoose.model( 'Project', ProjectSchema );
    
    //////////////////////////////////////////////////////////////////////////////
    
    const PORT = ( process.env.PORT || 1337 );
    const express = require( 'express' );
    const app = express();
    
    const bodyParser = require( 'body-parser' );
    const router = express.Router();
    
    router.post( '/project/create',
      bodyParser.json(),
      ( req, res ) => {
        console.log( 'request body:', req.body );
    
        const title = req.body.title;
        const description = req.body.description;
    
        Project.create( { title, description } )
        .then( doc => res.json( doc ) )
        .catch( err => res.json( err ) );
      }
    );
    
    app.use( router );
    app.listen( PORT, () => console.log( `now listening on port ${PORT}` ) );
    

    package.json:

    {
      "name": "stack-overflow",
      "main": "index.js",
      "scripts": {
        "start": "nodemon"
      },
      "dependencies": {
        "body-parser": "^1.15.2",
        "express": "^4.14.0",
        "mongodb": "^2.2.11",
        "mongoose": "^4.6.5"
      },
      "devDependencies": {
        "nodemon": "^1.11.0"
      }
    }
    

    docker-compose.yml:

    mongo:
      image: 'mongo:3'
    node:
      image: 'node:5.9.1'
      command: 'npm start'
      working_dir: '/var/app'
      links:
      - mongo
      volumes:
      - '.:/var/app'
      ports:
      - '1337:1337'
    

    example request:

    curl -X "POST" "http://localhost:1337/project/create" \
     -H "Content-Type: application/json; charset=utf-8" \
     -d "{\"title\":\"testing\",\"description\":\"just another description\"}"
    
    0 讨论(0)
提交回复
热议问题