gpt4 book ai didi

node.js - Mongoose :子文档 CastError:转换为数组失败

转载 作者:可可西里 更新时间:2023-11-01 09:55:26 27 4
gpt4 key购买 nike

我有两个模式:架构一(父级):

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 服务器为该文件创建了一个新条目(幻灯片集),它应该为每个页面创建一个幻灯片并将其存储到幻灯片集中。最后,整个事情应该保存到数据库中:

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);
}
});

“保存”函数抛出以下错误:

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]
});

?

关于node.js - Mongoose :子文档 CastError:转换为数组失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31851414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com