1

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?

1 Answer 1

1

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]
});
Sign up to request clarification or add additional context in comments.

1 Comment

Switching to Schema.Types.Mixed did the trick! The Mongoose docs claim that Schema.Types.Mixed, Object, and {} are equivalent, but apparently that's not the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.