2017-12-12 6 views
1

私のモデルであるStuffのオブジェクトの配列を更新しようとしています。Mongooseでオブジェクトの配列を更新する

マイモデル:私のルートで

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

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); 
}) 

これは、エラーがスローされます。

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

にconsole.logの出力は次のとおりです。

私だけではなく、名前フィールドを更新する場合は、しかし、

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); 
}) 

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

これは、適切にスタッフのドキュメントと更新オブジェクトの配列をハードコーディング

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

は、同じ結果を与えますconsole.logの出力は次のとおりです。

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

私は何が欠けていますか?

答えて

1

エラーNo default engine was specified and no extension was providedは、ビューをレンダリングしたいときは、その拡張子を持つ少なくともファイルを提供する必要があることを意味します

res.render('index.html'); 

それともはこのように、デフォルトのビューエンジンを設定します。

app.set('view engine', 'pug'); 
Mixedへのご StuffSchema変更 Objectタイプでも

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

'Schema.Types.Mixed'に切り替えると、そのトリックが実行されました! Mongooseのドキュメントでは、 'Schema.Types.Mixed'、' Object'、 '{}'は同等ですが、明らかにそうではありません。 – Devin

関連する問題