我正嘗試將嵌入文檔添加到現有文檔字段。我發現one fitting answer與搜索,但我遇到了錯誤。我正在使用node.js,Express和Mongoose。將文檔添加到嵌入文檔數組
我的數據庫模式:
var entry = new Schema({
name : { type : String, required : true},
description : { type : String, default: ""},
});
var compo = new Schema({
name : String,
description : String,
entries : [entry]
});
我試圖更新條目陣列用下面的代碼
var entry = new entryModel();
entry.name = "new name";
entry.description= "new description";
compoModel.findOne(query, function (err, item) {
if (item) {
item.entries.push(entry);
item.save(function (err) {
if (!err) {
log.debug('Entry added successfully.');
} else {
log.error("Mongoose couldn't save entry: " + err);
}
});
}
});
則產生了一個錯誤:TypeError: Object.keys called on non-object
有什麼我錯過了?