我有两个架构: Schema one(父级):
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服务器为文件创建一个新条目(幻灯片组),对于每个页面,它应创建幻灯片并将其存储到幻灯片组中。最后整个事情应该保存到db:
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上找到的不同可能的解决方案,但没有一个能够正常工作。我希望你能帮助我: - )
答案 0 :(得分:0)
你试过了吗?
var Audio = new Schema({
name: String,
dataID: Schema.Types.ObjectId
});
然后
slideSchema.add({
no: Number,
depth: Number,
audio: [Audio]
});