[英]autoValue in SimpleSchema for update does not set given value
我在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 : "-"
您應該處理兩種情況:
文件insert
用空(未定義) value
用於fileNo
。
使用fileNo
空value
update
文檔。
在第二種情況下,如果其值為空字符串( optional: true
),驗證器將嘗試從文檔中刪除該字段。 我們可以抓住並防止這種情況。
fileNo: {
type: String,
label: 'File No.',
autoValue() {
if (this.isInsert && (this.value == null || !this.value.length)) {
return '-';
}
if (this.isUpdate && this.isSet && this.operator === '$unset') {
return { $set: '-' };
}
},
optional: true
}
補充 :用於編寫此答案的文檔(代碼按預期工作;在本地測試):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.