SimpleSchema中用于更新的autoValue未设置给定值

时间:2017-10-05 11:19:49

标签: javascript meteor ecmascript-6 meteor-autoform simple-schema

我在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
  },

我正在使用autoformupdate字段。问题是当我用空字符串更新字段(即保持文本框为空)时,在上面的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 : "-"

1 个答案:

答案 0 :(得分:0)

你应该处理两种情况:

  1. 文档<= 17包含insert的空(未定义)value

  2. 文档fileNoupdate value

  3. 在第二种情况下,如果其值为空字符串(fileNo),验证器将尝试从文档中删除该字段。我们可以抓住并防止这种情况发生。

    optional: true

    已添加:用于撰写此答案的文档(代码按预期工作;在本地测试):