简体   繁体   English

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

[英]autoValue in SimpleSchema for update does not set given value

I am using below field in SimpleSchema, 我在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
  },

I am using autoform to update a field. 我正在使用autoform update字段。 The problem is when I am updating the field with empty string (ie keeping textbox empty) , the value - is not set as per field definition in SimpleSchema code above. 问题是当我使用空字符串更新字段(即,保持文本框为空)时 ,未根据上面的SimpleSchema代码中的字段定义设置值-

I get logs as below, 我得到如下日志

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.    

and my collection document does not have field fileNo . 我的收款文件没有栏位fileNo

What am I missing? 我想念什么? all I want is when value of fileNo is empty/undefined value must be set to - (hyphen) . 我想要的就是fileNo值为empty/undefined值必须设置为- (连字符) In document it must look like fileNo : "-" 在文档中,它必须类似于fileNo : "-"

You should handle 2 situations: 您应该处理两种情况:

  1. Document insert with empty (undefined) value for fileNo . 文件insert用空(未定义) value用于fileNo

  2. Document update with empty value for fileNo . 使用fileNovalue update文档。

In second case, validator will try to remove the field from the document if its value is empty string ( optional: true ). 在第二种情况下,如果其值为空字符串( optional: true ),验证器将尝试从文档中删除该字段。 We can catch and prevent this. 我们可以抓住并防止这种情况。

  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
  }

Added : documentation used to write this answer (code works as expected; tested locally): 补充 :用于编写此答案的文档(代码按预期工作;在本地测试):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 流星服务器在SimpleSchema autoValue选项上崩溃 - Meteor server is crashing on SimpleSchema autoValue option 如何在SimpleSchema中使用布尔值将false设置为值? - How to set false as a value using Boolean in SimpleSchema? 流星集合Simpleschema,自动值取决于其他字段值 - Meteor Collections Simpleschema, autovalue depending on other field values 如何通过autoValue将Meteor.userId()添加到SimpleSchema? - How to add Meteor.userId() to SimpleSchema via autoValue? AutoForm中没有使用Meteor方法设置AutoValue - AutoValue not set in AutoForm with Meteor method 如何在SimpleSchema Meteor中定义一个具有自动值的子文档,而不将其插入每个父文档插入中? - How to define a subdocument in SimpleSchema Meteor with an autovalue without inserting it on each parent document insert? 如何告诉SimpleSchema更新当前的Meteor.user? - How do I tell SimpleSchema to update the current Meteor.user? MongoDB +猫鼬:仅在给定键不存在或值伪造时设置$ set - MongoDB + Mongoose: $set only if given key does not exist or have falsy value 在给定的元素集中未获得最小属性值 - Not getting min attribute value in given elements set 如何使用数组中给定的一组规则验证值? - how to validate a value with a set of rules given in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM