具有嵌套自动值的 SimpleSchema 无效键
SimpleSchema invalid keys with nested autoValue
我有这样的模式(剪掉绒毛):
Schemas.people = new SimpleSchema({
note: {
type: [Schemas.notes],
optional: true,
defaultValue: []
},
updates: {
type: [Schemas.updates],
optional:true,
autoValue:function(){
if (this.isInsert) {
return [{
at: new Date,
user_id: this.userId
}];
}
return {
$push:{
at: new Date,
user_id: this.userId
}
}
}
}
});
笔记架构如下所示:
Schemas.notes = new SimpleSchema({
note: {
type: String,
autoform: {
afFieldInput:{
type:"textarea"
}
},
optional: true
},
updates: {
type: [Schemas.updates],
optional:true,
autoform:{
omit:true
},
autoValue:function(){
if (this.isInsert) {
return [{
at: new Date,
user_id: this.userId
}];
}
return {
$push:{
at: new Date,
user_id: this.userId
}
}
}
}
});
而且更新模式非常简单:
Schemas.updates = new SimpleSchema({
at: {
type: Date
},
user_id:{
type: Meteor.ObjectID
}
});
people
架构上的 "updates" 字段在进行更新时按预期保存 date/user id。但是,它在 notes
架构上失败:
SimpleSchema invalid keys for "blablabla" context:
0: Object
name: "note.0.updates.0.at"
type: "keyNotInSchema"
value: Mon May 11 2015 11:57:58 GMT-0400 (Eastern Daylight Time)
1: Object
name: "note.0.updates.0.user_id"
type: "keyNotInSchema"
value: "abcd1234"
我相信 "name" 应该看起来像 "people.note.0.updates.0.at" 但我不确定这个假设是否正确,我完全不确定如何实现这一点。
更新:
用于更新的代码people
{{#autoForm collection="people" id=formId type="update" class="update" autocomplete="off" doc=getDocument autosave=true template="quickform"}}
{{> afQuickField name='note' template="quickform" }}
{{/autoForm}}
formId
returns 一个随机 ID 字符串并且 getDocument
传入正确的集合。
Schemas.notes._schemaKeys
不列出 at
和 user_id
字段...但 Schemas.people._schemaKeys
列出。
人物架构显示:[..., "updates.$.at", "updates.$.user_id", ...]
Notes 架构显示:["note"、"updates"、"updates.$"]
真奇怪。
问题在于架构的声明顺序。我想这有道理吗?我在 "updates" 之前声明 "notes",最后声明 "people"。把 "updates" 首先完全解决了这个问题。
我将把这作为一个可能的错误报告给集合存储库。
请注意,Meteor 使用标准 JavaScript 语法,因此具有相同的限制,例如您已经意识到代码的顺序很重要。
一起来看看
Schemas.notes = new SimpleSchema({
updates: {
type: [Schemas.updates]
}
}
此代码中没有嵌套函数,因此在 Meteor 继续下一个 Schema 定义之前,将执行每一行代码。 Schema.updates
将立即取消引用,尽管尚未设置。 type
将是一个包含 null
的数组,这最终使 SimpleSchema 假设根本不允许任何字段。
我有这样的模式(剪掉绒毛):
Schemas.people = new SimpleSchema({
note: {
type: [Schemas.notes],
optional: true,
defaultValue: []
},
updates: {
type: [Schemas.updates],
optional:true,
autoValue:function(){
if (this.isInsert) {
return [{
at: new Date,
user_id: this.userId
}];
}
return {
$push:{
at: new Date,
user_id: this.userId
}
}
}
}
});
笔记架构如下所示:
Schemas.notes = new SimpleSchema({
note: {
type: String,
autoform: {
afFieldInput:{
type:"textarea"
}
},
optional: true
},
updates: {
type: [Schemas.updates],
optional:true,
autoform:{
omit:true
},
autoValue:function(){
if (this.isInsert) {
return [{
at: new Date,
user_id: this.userId
}];
}
return {
$push:{
at: new Date,
user_id: this.userId
}
}
}
}
});
而且更新模式非常简单:
Schemas.updates = new SimpleSchema({
at: {
type: Date
},
user_id:{
type: Meteor.ObjectID
}
});
people
架构上的 "updates" 字段在进行更新时按预期保存 date/user id。但是,它在 notes
架构上失败:
SimpleSchema invalid keys for "blablabla" context:
0: Object
name: "note.0.updates.0.at"
type: "keyNotInSchema"
value: Mon May 11 2015 11:57:58 GMT-0400 (Eastern Daylight Time)
1: Object
name: "note.0.updates.0.user_id"
type: "keyNotInSchema"
value: "abcd1234"
我相信 "name" 应该看起来像 "people.note.0.updates.0.at" 但我不确定这个假设是否正确,我完全不确定如何实现这一点。
更新:
用于更新的代码people
{{#autoForm collection="people" id=formId type="update" class="update" autocomplete="off" doc=getDocument autosave=true template="quickform"}}
{{> afQuickField name='note' template="quickform" }}
{{/autoForm}}
formId
returns 一个随机 ID 字符串并且 getDocument
传入正确的集合。
Schemas.notes._schemaKeys
不列出 at
和 user_id
字段...但 Schemas.people._schemaKeys
列出。
人物架构显示:[..., "updates.$.at", "updates.$.user_id", ...]
Notes 架构显示:["note"、"updates"、"updates.$"]
真奇怪。
问题在于架构的声明顺序。我想这有道理吗?我在 "updates" 之前声明 "notes",最后声明 "people"。把 "updates" 首先完全解决了这个问题。
我将把这作为一个可能的错误报告给集合存储库。
请注意,Meteor 使用标准 JavaScript 语法,因此具有相同的限制,例如您已经意识到代码的顺序很重要。
一起来看看
Schemas.notes = new SimpleSchema({
updates: {
type: [Schemas.updates]
}
}
此代码中没有嵌套函数,因此在 Meteor 继续下一个 Schema 定义之前,将执行每一行代码。 Schema.updates
将立即取消引用,尽管尚未设置。 type
将是一个包含 null
的数组,这最终使 SimpleSchema 假设根本不允许任何字段。