I am trying to return specific status codes such as, 409 Conflict. I have used the Model#save docs
Edit: I am not trying to solve the error, it is deliberate.
According to the docs, there is three parameters on the callback: err, product, and numAffected.
EDIT: I wrote this code wrong and I edited. Either way, I got an excellent answer from Ryan.
app.post('/skill', (req, res) => {
const skill = new Skill({some_duplicate_object});
skill.save((err, product, numAffected) => {
console.log("Error: ", err);
});
NOT my console.log, in the Mocha test cli, I get an error:
(node:19760) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 5): ValidationError: Path `name` is required.
By playing around and shear luck, I did this: This is NOT in the mongoose docs and is my main reason for writing this post.
app.post('/skill', (req, res) => {
const skill = new Skill({});
skill.save()
.then((err, product, numAffected) => {
console.log("Nothing displayed here");
}, (err) => {
console.log(err.errors);
});
Even though this is not in the docs, it shows the error I want. As someone that is REALLY trying to use official docs more, I find it so hard to understand what is really going on. Why does this work and if it is in the docs, where would this info be?
{ name:
{ MongooseError: Path `name` is required.
at ValidatorError (/home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/error/validator.js:24:11)
at validate (/home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/schematype.js:706:13)
at /home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/schematype.js:752:11
at Array.forEach (native)
at SchemaString.SchemaType.doValidate (/home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/schematype.js:712:19)
at /home/codeamend/Coding/projects/portfolio/work/CodeAmend.Com/backend/node_modules/mongoose/lib/document.js:1408:9
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
message: 'Path `name` is required.',
name: 'ValidatorError',
properties:
{ type: 'required',
message: 'Path `{PATH}` is required.',
validator: [Function],
path: 'name',
value: undefined },
kind: 'required',
path: 'name',
value: undefined,
reason: undefined } }
Extra info:
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"request": "^2.81.0",
"supertest": "^3.0.0"
},
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"mongoose": "^4.9.2"
}