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
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\"}"