Meteor Autoform `this.field("keyName").value` returns undefined in Schema
Meteor Autoform `this.field("keyName").value` returns undefined in Schema
我有一个 Person
,我希望在有人更改他们的 firstName
或 lastName
字段时自动创建一个 fullName
字段和值。
People.attachSchema(new SimpleSchema({
firstName: {
type: String,
optional: false,
label: 'First Name'
},
lastName: {
type: String,
optional: false,
label: 'Last Name'
},
fullName: {
type: String,
optional: false,
label: 'Full Name',
autoValue: function() {
var firstName = this.field("firstName");
var lastName = this.field("lastName");
if(firstName.isSet || lastName.isSet){
return firstName.value + " " + lastName.value;
} else {
this.unset();
}
}
}
}));
如果我那么做
People.update("ZqvBYDmrkMGgueihX", {$set: {firstName: "Bill"}})
fullName
设置为Bill undefined
我做错了什么?
现在,您的条件 firstName.isSet || lastName.isSet
表示,如果 firstName
或 lastName
之一在文档中可用,则创建 fullName
。因此它正确地分配了全名,因为其中一个已设置(名字设置为 Bill)。您需要像这样使用 &&
而不是 ||
。
People.attachSchema(new SimpleSchema({
firstName: {
type: String,
optional: false,
label: 'First Name'
},
lastName: {
type: String,
optional: false,
label: 'Last Name'
},
fullName: {
type: String,
optional: false,
label: 'Full Name',
autoValue: function() {
var firstName = this.field("firstName");
var lastName = this.field("lastName");
if(firstName.isSet && lastName.isSet){
return firstName.value + " " + lastName.value;
} else {
this.unset();
}
}
}
}));
但是,根据你的问题,我认为,你想设置 fullName
只有当 firstName
或 lastName
之一更新时,我想(我不确定),需要检查this.field('firstName').operator
,检查当前操作是否为$set
我有一个 Person
,我希望在有人更改他们的 firstName
或 lastName
字段时自动创建一个 fullName
字段和值。
People.attachSchema(new SimpleSchema({
firstName: {
type: String,
optional: false,
label: 'First Name'
},
lastName: {
type: String,
optional: false,
label: 'Last Name'
},
fullName: {
type: String,
optional: false,
label: 'Full Name',
autoValue: function() {
var firstName = this.field("firstName");
var lastName = this.field("lastName");
if(firstName.isSet || lastName.isSet){
return firstName.value + " " + lastName.value;
} else {
this.unset();
}
}
}
}));
如果我那么做
People.update("ZqvBYDmrkMGgueihX", {$set: {firstName: "Bill"}})
fullName
设置为Bill undefined
我做错了什么?
现在,您的条件 firstName.isSet || lastName.isSet
表示,如果 firstName
或 lastName
之一在文档中可用,则创建 fullName
。因此它正确地分配了全名,因为其中一个已设置(名字设置为 Bill)。您需要像这样使用 &&
而不是 ||
。
People.attachSchema(new SimpleSchema({
firstName: {
type: String,
optional: false,
label: 'First Name'
},
lastName: {
type: String,
optional: false,
label: 'Last Name'
},
fullName: {
type: String,
optional: false,
label: 'Full Name',
autoValue: function() {
var firstName = this.field("firstName");
var lastName = this.field("lastName");
if(firstName.isSet && lastName.isSet){
return firstName.value + " " + lastName.value;
} else {
this.unset();
}
}
}
}));
但是,根据你的问题,我认为,你想设置 fullName
只有当 firstName
或 lastName
之一更新时,我想(我不确定),需要检查this.field('firstName').operator
,检查当前操作是否为$set