Javascript SimpleSchema for update中的autoValue未设置给定值

Javascript SimpleSchema for update中的autoValue未设置给定值,javascript,meteor,ecmascript-6,meteor-autoform,simple-schema,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

我在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
更新一个字段。问题是,当我用空字符串更新字段时(即保持文本框为空),值
-
没有按照上面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
的值为
空/未定义时,必须将
-
(连字符)
设置为
-
。在文档中,它必须看起来像
fileNo:“-”
您应该处理两种情况:

  • 文档
    文件编号插入带有空(未定义)
    值的
    文档

  • 文档
    更新
    ,对于
    文件编号
    ,使用空的

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

    fileNo:{
    类型:字符串,
    标签:“文件号”,
    自动值(){
    if(this.isInsert&(this.value==null | | |!this.value.length)){
    返回“-”;
    }
    if(this.isUpdate&&this.isSet&&this.operator==='$unset'){
    返回{$set:'-'};
    }
    },
    可选:true
    }
    
    添加了:用于编写此答案的文档(代码按预期工作;在本地测试):


    您应该处理两种情况:

  • 文档
    文件编号插入带有空(未定义)
    值的
    文档

  • 文档
    更新
    ,对于
    文件编号
    ,使用空的

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

    fileNo:{
    类型:字符串,
    标签:“文件号”,
    自动值(){
    if(this.isInsert&(this.value==null | | |!this.value.length)){
    返回“-”;
    }
    if(this.isUpdate&&this.isSet&&this.operator==='$unset'){
    返回{$set:'-'};
    }
    },
    可选:true
    }
    
    添加了:用于编写此答案的文档(代码按预期工作;在本地测试):



    你到底想达到什么目的?@Styx:用粗体修改了最后一行。提前谢谢。也许,
    defaultValue
    是您所需要的?这也不起作用。由于
    this.isSet
    true
    ,因此它已经接受了
    空的
    字符串,因此它不使用
    defaultValue
    。您使用的是
    简单模式的哪个版本?您到底想实现什么?@Styx:用粗体修改了最后一行。提前谢谢。也许,
    defaultValue
    是您所需要的?这也不起作用。由于
    this.isSet
    true
    ,因此它已经接受了
    空的
    字符串,因此它不使用
    defaultValue
    。您使用的是
    简单模式的哪个版本?再次感谢。你节省了我很多时间,这是一个我无法解决的非常小的问题:)@AnkurSoni不客气:)彻底阅读文档非常有帮助;)你能在回答中指出文档的链接和行号吗?@AnkurSoni更新了我的答案你可以签约吗?再次感谢。你节省了我很多时间,这是一个我无法解决的非常小的问题:)@AnkurSoni不客气:)彻底阅读文档非常有帮助;)你能在回答中指出文档的链接和行号吗?@AnkurSoni更新了我的答案你可以签约吗?