如何在Meteor中为嵌套数组定义模式?
我正在使用SimpleSchema和meteor来构造数据库条目。
问题是,我有一个数组的数组和定义的模式不起作用。
这里是一个文档的例子:
Courses.insert({ "persian_title":"persian title", "english_title":"english title", "free_for_all":true, "price":1000, "is_offer":false, "offer_price":500, "Seasons":[ { "title":"first Season", "free_for_all":false, "Episodes":[ { "title":"first Episode", "length":"12:10:00", "url":"test" }, { "title":"second Episode", "length":"0:10:00", "url":"test" }, { "title":"third Episode", "length":"14:10:00", "url":"test" } ] }, { "title":"Second Season", "free_for_all":false, "Episodes":[ { "title":"first Episode", "length":"12:10:00", "url":"test" }, { "title":"second Episode", "length":"0:10:00", "url":"test" }, { "title":"third Episode", "length":"14:10:00", "url":"test" } ] } ] })
和架构:
Courses = new Mongo.Collection("courses"); var Schemas = {}; Schemas.Courses = new SimpleSchema( { persian_title: { type: String }, english_title: { type: String }, free_for_all: { type: Boolean }, price: { type: Number }, is_offer: { type: Boolean }, offer_price: { type: Number }, // Seasons "Courses.$.Seasons": { type: [Object] }, "Courses.$.Seasons.$.title": { type: String }, "Courses.$.Seasons.$.free_for_all": { type: Boolean }, // Episodes "Courses.$.Seasons.$.Episodes": { type: [Object] }, "Courses.$.Seasons.$.Episodes.title": { type: String }, "Courses.$.Seasons.$.Episodes.length": { type: String, max: 8 }, "Courses.$.Seasons.$.Episodes.url": { type: String, max: 1000 } }); Courses.attachSchema(Schemas.Courses);
简单模式文档: https : //github.com/aldeed/meteor-simple-schema#schema-keys
问题是如何为数组数组定义模式?
您必须显式定义Seasons.$.Episodes
您的架构中的type: [Object]
Seasons.$.Episodes
。 定义Seasons对象的数组模式,如下所示:
Courses = new Mongo.Collection("courses"); var Schemas = {}; Schemas.Courses = new SimpleSchema( { persian_title: { type: String }, english_title: { type: String }, free_for_all: { type: Boolean }, price: { type: Number }, is_offer: { type: Boolean }, offer_price: { type: Number }, // Seasons "Seasons": { type: [Object] }, "Seasons.$.title": { type: String }, "Seasons.$.free_for_all": { type: Boolean }, // Episodes "Seasons.$.Episodes": { type: [Object] }, "Seasons.$.Episodes.$.title": { type: String }, "Seasons.$.Episodes.$.length": { type: String, max: 8 }, "Seasons.$.Episodes.$.url": { type: String, max: 1000 } }); Courses.attachSchema(Schemas.Courses);
- Meteor方法对客户端返回undefined(asynchronous)
- 当第三方软件更新数据库时,Node.JS和/或Meteor如何从数据库中获取callback
- 铁路由器使用Meteor.call抛出的动作writeHead不是一个函数
- 针对vsivsi / meteor-job-collection的Node.js worker在job.done()上失败:“调用方法job_jobDone时出现exception”错误:匹配错误…“
- 处理meteor客户端的错误
- 在meteor中通过路由器填充模板
- npm ERR! 404没有findnpm ERR! 不好的代码0
- 在Raspberry pi上运行meteor(ARM6)
- 如何正确configurationmeteor与节点file upload模块'multer'