我有两种模式:
架构一(父级):
var slide = require('../model/slide');
var slidesetSchema = new Schema({
name: String,
dataID: Schema.Types.ObjectId,
slides: [slide]
});
子模式:
var slideSchema = new Schema();
// use this method for a recursive definiton
// src https://groups.google.com/forum/#!topic/mongoose-orm/0yUVXNyprx8
slideSchema.add({
no: Number,
depth: Number,
audio: [{
name: String,
dataID: Schema.Types.ObjectId
}],
//subslides: [slideSchema] <- this will be the next step if the other problem is solved
// it will be used to create a tree like structure
});
我有以下情况:用户上传一个pdf文件,然后nodejs服务器为该文件创建一个新条目(slideset),对于每个页面,它应该创建一个幻灯片并将其存储到slideset中。最后,整个事情应该保存到数据库中:
var set = new slideset();
var slides = [];
for (var i = 1; i <= pagecount; i++) {
var s = new slide({no: i, depth: 1, audio: []});
slides.push(s);
// set.slides.push(s); <-- throws "can't call push of undefined",
// well here http://mongoosejs.com/docs/subdocs.html they just do the same thing.. why doesn't it work for me?!
}
set.slides = slides;
// some other stuff, like writestream to save the file into db, set set.name and dataID
writestream.on('close', function (file) {
// save schema
set.save(function (err) {
console.err(err);
}
});
函数“save”引发以下错误:
ValidationError: CastError: Cast to Array failed for value "{...},{...},..." at path "slides"
我在stackoverflow上尝试了不同的可能解决方案,但都没有成功。我希望你能帮助我:-)
最佳答案
你试过了吗:
var Audio = new Schema({
name: String,
dataID: Schema.Types.ObjectId
});
然后
slideSchema.add({
no: Number,
depth: Number,
audio: [Audio]
});
?