I have two schemas:
Schema one (the parent):
var slide = require('../model/slide');
var slidesetSchema = new Schema({
name: String,
dataID: Schema.Types.ObjectId,
slides: [slide]
});
and the child schema:
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
});
I have the following situation: the user uploads a pdf file and then my nodejs server creates a new entry (slideset) for the file and for each page it should create a slide and store it into the slideset. Finally the whole thing should be saved into the 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);
}
});
the function ‘save’ throws the following error:
ValidationError: CastError: Cast to Array failed for value "{...},{...},..." at path "slides"
I tried different possible solutions I’ve found here on stackoverflow but none of them worked. I hope you can help me 🙂