derjulezzz derjulezzz - 1 year ago 129
Javascript Question

_.extend ignores a property in MEAN Stack

I'm trying to overwrite an existing Object in my MongoDB. It worked fine for month. Now my _.extend() ignores a property. The Schema for the Document is:

var KlausurSchema = new Schema(
name: String,
pruefer: {
eins: String,
zwei: String
datum: {
start: Date,
dauer: Number
notenspiegel: {
von: [Number],
bis: [Number]
aufgaben: [{
name: String,
punkte: Number
studenten: [{
matrnr: Number,
status: String, //Krank/Abgemdelet/Verfügbar
vorname: String,
nachname: String,
bewertung: Number,
pversuch: Number,
pvermerk: String,
freiverm: String,
labnr: Number,
porgnr: Number,
aenddat: String,
punkte: [],
gesamtPunkte: Number,
stapel: Number,
studiengang: String,
importID: String, //um einzelne Importe zu löschen
korrekturAnzahl: Number //um Nachkorrigierte zu exportieren
imports: [{
id: String,
datum: Date,
studiengang: String,
anzahl: Number
hoersaele: [Object],
aenderdat: Date


I do the following. Using a PUT-Method i load this function:

exports.put = function (req, res){
Klausur.load(req.params.klausurId, function (err, klausur) {

klausur = _.extend(klausur, req.body);
console.log(klausur); (err) {


Like i said, it worked fine. But if i input this (output from req.body):

{ _id: '5809da4c3a81b8341abf8f51',
name: 'Analysis',
aenderdat: '2016-10-21T09:05:16.350Z',
__v: 4,
[ { _id: '58074b1f495b58d018116983',
name: 'Aula2',
kapazitaet: 10,
puffer: 2,
adresse: 'isauhsda',
hausmeister: 'sydkl',
telefon: '203',
aenderdat: '2016-10-21T08:03:44.660Z',
__v: 0,
belegung: [Object] } ],
[ { id: 'i1e6rdi0lq',
datum: '2016-10-21T09:05:39.886Z',
studiengang: 'Maschi',
anzahl: 3,
_id: '5809da633a81b8341abf8f63' },
{ id: 'gjd84dtgho',
datum: '2016-10-21T09:16:34.429Z',
studiengang: 'WirtIng',
anzahl: 6,
_id: '5809dcf23a81b8341abf8f67' } ],
studenten: ...

it exports shows me this (output from klausur after _.extend()):

[ { _id: 5809da633a81b8341abf8f63,
anzahl: 3,
studiengang: 'Maschi',
datum: Fri Oct 21 2016 11:05:39 GMT+0200 (Mitteleuropäische Sommerzeit),
id: 'i1e6rdi0lq' },
{ _id: 5809dcf23a81b8341abf8f67,
anzahl: 6,
studiengang: 'WirtIng',
datum: Fri Oct 21 2016 11:16:34 GMT+0200 (Mitteleuropäische Sommerzeit),
id: 'gjd84dtgho' } ],
hoersaele: [],
__v: 4,
aenderdat: Fri Oct 21 2016 11:05:16 GMT+0200 (Mitteleuropäische Sommerzeit),
name: 'Analysis',
_id: 5809da4c3a81b8341abf8f51 }

I left away unimportant properties. I just wanted to show you that the "Hoersaele" property does not get transferred to the new object which gets uploaded to the DB.

MongoDB gives me this error msg:

{ [ValidationError: Klausur validation failed]
message: 'Klausur validation failed',
name: 'ValidationError',
{ hoersaele:
{ [CastError: Cast to Array failed for value "[object Object]" at path "hoersaele"]
message: 'Cast to Array failed for value "[object Object]" at path "hoersaele"',
name: 'CastError',
kind: 'Array',
value: [Object],
path: 'hoersaele',
reason: undefined } } }

Any suggestions? Thank you!

Answer Source

I see more than one possible explanations or mistakes:

1. In your Schema you define hoersaele: [Object] but Mongoose does not define Object type: SchemaTypes.

For no validation you should use: hoersaele: [Schema.Types.Mixed].

Or maybe define another schema for that and use it inside: hoersaele: [HoersaeleSchema].

Or maybe store them in another collection and just reference them and use the populate feature of Mongoose when you want to retrieve the full model: hoersaele: [Schema.Types.ObjectId].

2. It seems you try to extend the Document which is a native object with the req.body which is a plain javascript object.

If the point is to merge them in a plain javascript object you should use Document#toObject([options]) in order to retrieve the plain javascript object.

This code should work:

let mergedKlausur = _.extend(klausur.toObject(), req.body);

3. If you want to update the existing model with req.body I think you should just use:

klausur.update({$set: req.body});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download