因此我在var array
上添加新章节时遇到问题,我将如何执行此操作:
array.push({
chapter: [
{
id: 2,
title: 'adsf',
content: '',
authorNotes: 'asdf'
}
]
});
RiTest.ts
import * as mongoose from 'mongoose';
const Scheme = mongoose.Schema;
export const RiTestScheme = new Scheme({
novelName: String,
novelAuthor: String,
novelCoverArt: String,
novelTags: Array,
chapters: [
{
id: Number,
title: String,
content: String,
authorNotes: String
}
]
});
export class RiTestController {
public addChapter(callback: (data) => void) {
var chapterInfoModel = mongoose.model('ChaptersTest', RiTestScheme);
var array = [
{
chapter: [
{
id: 0,
title: 'prolog',
content: 'conetntt is empty',
authorNotes: 'nothing is said by author'
},
{
id: 1,
title: 'making a sword',
content: 'mine craft end chapter',
authorNotes: 'nothing'
}
]
}
];
let newChapterInfo = new chapterInfoModel(array);
newChapterInfo.save((err, book) => {
if (err) {
return callback(err);
} else if (!err) {
return callback(book);
}
});
}
}
这不起作用,var array
未保存到let newChapterInfo = new chapterInfoModel(array);
中,我正在尝试将另一章添加到array.chapter
中,但数组未在chapterInfoModel()
我将如何修复此数组并向该数组中添加一个项目,以在此现有集合中创建一个新条目
感谢您抽出宝贵时间回答我的问题。
答案 0 :(得分:1)
您正在尝试将文档数组插入到您的收藏夹中,这就是它没有插入到您的收藏夹中的原因。
Document.prototype.save()将仅向您的收藏夹插入一个文档,具体取决于您的定义。因此,在下面的代码中插入chapter
,
//array as Object
var array = {
chapter: [
{
id: 0,
title: 'prolog',
content: 'conetntt is empty',
authorNotes: 'nothing is said by author'
},
{
id: 1,
title: 'making a sword',
content: 'mine craft end chapter',
authorNotes: 'nothing'
}
]
};
//Push to your chapter array
array.chapter.push({
id: 2,
title: 'adsf',
content: '',
authorNotes: 'asdf'
});
let newChapterInfo = new chapterInfoModel(array);