【发布时间】:2018-03-16 23:56:58
【问题描述】:
我在 SimpleSchema 中使用以下字段,
"fileNo": {
type: String,
label: "File No.",
autoValue: function() {
console.log('this', this);
console.log('typeof this.value.length ', typeof this.value.length);
console.log('this.value.length ', this.value.length, this.value.length === 0, this.value.length == 0);
console.log('this.value ', this.value, typeof this.value);
console.log('this.operator ', this.operator);
console.log('this.isSet ', this.isSet);
if ( this.isSet && 0 === this.value.length ) {
console.log('if part ran.');
return "-";
} else {
console.log('else part ran.');
}
},
optional:true
},
我正在使用autoform 到update 一个字段。问题是当我使用空字符串 (即保持文本框为空) 更新字段时,值 - 未按照上面 SimpleSchema 代码中的字段定义设置。
我得到如下日志,
clients.js:79 this {isSet: true, value: "", operator: "$unset", unset: ƒ, field: ƒ, …}
clients.js:80 typeof this.value.length number
clients.js:81 this.value.length 0 true true
clients.js:82 this.value string
clients.js:83 this.operator $unset
clients.js:84 this.isSet true
clients.js:86 if part ran.
clients.js:79 this {isSet: true, value: "-", operator: "$unset", unset: ƒ, field: ƒ, …}
clients.js:80 typeof this.value.length number
clients.js:81 this.value.length 1 false false
clients.js:82 this.value - string
clients.js:83 this.operator $unset
clients.js:84 this.isSet true
clients.js:89 else part ran.
而我的收藏文档没有字段fileNo。
我错过了什么?我想要的只是当fileNo 的值为empty/undefined 时,值必须设置为-(连字符)。在文档中它必须看起来像fileNo : "-"
【问题讨论】:
-
你到底想达到什么目的?
-
@Styx:用粗体修改最后一行。提前致谢。
-
也许,
defaultValue是您所需要的? -
那也行不通。因为
this.isSet是true,所以它已经在使用empty字符串,所以它不使用defaultValue。 -
你使用的是什么版本的
simple-schema?
标签: javascript meteor ecmascript-6 meteor-autoform simple-schema