Devin
Devin

Reputation: 505

Updating an array of objects with Mongoose

I'm trying to update an array of objects on my model, Stuff.

My model:

var StuffSchema = new Schema({
  name: { type: String, required: true },
  things: [Object]
});

In my route:

Stuff.update({"_id": req.params.id}), {$push: {"things": {$each: req.body.things}}}, function(err, raw) {
  console.log(raw);
  if (err) throw err;
  res.json(true);
})

This throws the error:

Error: No default engine was specified and no extension was provided.

The output of the console.log is:

{ ok: 0, n: 0, nModified: 0 }

Hardcoding an array of objects gives the same results:

Stuff.update({"_id": req.params.id}), {$push: {"things": {$each: [{a: 1, b: 2}, {a: 3, b: 4}]}}}, function(err, raw) {
  console.log(raw);
  if (err) throw err;
  res.json(true);
})

However, if I instead just update the name field:

Stuff.update({"_id": req.params.id}), {"name": "fancy pants"}, function(err, raw) {
  console.log(raw);
  if (err) throw err;
  res.json(true);
})

This properly updates the Stuff document and the output of the console.log is:

{ n: 1, nModified: 0, opTime: { ts: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1513093848 }, t: 1 }, electionId: 7fffffff0000000000000001, ok: 1 }

What am I missing?

Upvotes: 1

Views: 101

Answers (1)

YouneL
YouneL

Reputation: 8369

The error No default engine was specified and no extension was provided means that when you want to render a view you have to provide at least a file with its extension :

res.render('index.html');

Or set a default view engine, like this:

app.set('view engine', 'pug');

Also in your StuffSchema change Object type to Mixed:

var StuffSchema = new Schema({
    name: { type: String, required: true },
    things: [Schema.Types.Mixed]
});

Upvotes: 1

Related Questions