Alex Alex - 5 months ago 8
Node.js Question

Mongoose create() brings unwanted changes to documents

I'm currently working on a MEAN based project, and I'm wiring the mongo + node part to AngularJS. I'm using Mongoose for schemas, which is very handy, but I'm encountering a strange behavior with nested schemas.
I'm creating a document in AngularJS, which I send to the Node.js part with a $http.post request. When receiving it, the server logic uses Model.create Mongoose function to insert it in the collection. It successfully does, but the create function is missing parts of the document, and replacing some nested objects by Booleans.

Here is the Node.js output when receiving the document:

{ title: 'First Try',
branch: 2,
app: { name: 'Agirc Arrco' },
time:
{ periodNumber: 26,
periodDisplay: '27/06/2016',
submitDate: 1467304996377 },
author: { name: 'Doe', surname: 'John', id: 0 },
meta:
{ read: false,
priority: 3,
color: 'light-green',
type: true,
id: 6,
gathered: false },
elements:
[ { title: 'Sample',
description: 'Sample',
priority: 3,
app: [Object],
color: 'light-green',
type: 1,
id: 0 } ],
holidayData:
{ start: '30/06/2016',
startDisplay: '30/06/2016',
end: '',
endDisplay: '' },
comments: '',
rights:
{ editable: { level: false, branch: false, people: [Object] },
restriction: { level: false, branch: false, people: [Object] } } }


Here is the output after the document was inserted in the collection:

{ __v: 0,
title: 'First Try',
meta: true,
comments: '',
_id: 57754c24b322186ef2ace2c0,
author: { name: 'Doe', surname: 'John', id: 0 },
rights:
{ restriction: { level: false, branch: false, people: [Object] },
editable: { level: false, branch: false, people: [Object] } },
elements: [ true ],
time:
{ periodNumber: 26,
periodDisplay: '27/06/2016',
submitDate: 1467304996377 },
app: { name: 'Agirc Arrco' } }


Here is the Mongoose schema:

var Report = mongoose.model('Report',{
title : String,
app : {name : String, id : Number},
time : {periodNumber : Number, periodDisplay : String, submitDate : Number},
meta : {
read : Boolean,
priority : Number,
color : String,
type : Boolean,
id : Number
},
elements : [{
title : String,
description : String,
type : Number,
priority : Number,
color : String,
app : {name : String, id : Number}
}],
rights : {
editable : {
level : Boolean,
branch : Boolean,
people : [{id : Number, bool : Boolean}]
},
restriction : {
level : Boolean,
branch : Boolean,
people : [{id : Number, bool : Boolean}]
}
},
comments : String,
author : {name : String, surname : String, id : Number}
});


And the server side logic:

app.post('/api/reports',function(req,res){
var report = req.body;
console.log(report);
Report.create(report, function(err,report){
if(err) res.send(err);
console.log('Created:');
console.log(report);
console.log('Error? ');
console.log(err);
//Reload reports
Report.find(function(err,reports){
if(err) res.send(err);
res.json(reports);
});
});
});


No error is thrown by Mongoose, as I get a 'null' output when displaying the err variable.
Any clue about that issue would be appreciated, I didn't find any case that matched this one...

Thank you!

Answer

You should not use Mongoose keywords as properties for fields in a Schema.
meta : { read : Boolean, priority : Number, color : String, type : Boolean, id : Number },
For example, 'type' indicates the type of a field in Mongoose, so, from the above snippet, mongoose assumes that the 'meta' field is a Boolean, because you specified that its 'type'. Just rename 'type' to something else.
Also, you do not have 'branch' and 'holidayData' in your Schema.