【问题标题】:how to set a default value to element in a collection of type [String]?如何为 [String] 类型的集合中的元素设置默认值?
【发布时间】:2016-06-22 05:49:32
【问题描述】:

单击提交按钮后,我有快速表单,此方法被触发

submitPost: function (app) {
    check(app, {
      title: String,
      description: String,
      category: String,
      price: Number
    });
    var knownId = Products.insert(app);
    Products.update({ _id: knownId }, { $set:{screenShots: scs, previewImage: pi, sourceCode: zip }});

  }

当我没有在集合中为“screenShots、previewImage 和 sourceCode”提供默认值时,提交按钮不起作用。

一旦我给了他们一个默认值,如下所示

previewImage: {
    type: String,
    defaultValue: "jjj",
  },
   sourceCode: {
    type: String,
    defaultValue: "jjj",
  },
  screenShots: {
    type: [String],
    autoValue: function() {
      return [];
    }
  },

现在表单中的提交按钮正在工作并触发更新方法。它会同时更新“previewImage 和 sourcecCode”,但“screenShots”仍然是空的。

我不确定,但我认为问题与 autoValue 有关,我应该将其设为默认值,但是如何为字符串数组类型的元素赋予默认值?

还是与其他问题有关?

【问题讨论】:

  • 我可以知道您使用哪个包进行架构设计吗?
  • @PankajJatav aldeed/meteor-collection2 如果我理解你的问题的话

标签: meteor meteor-autoform meteor-collection2


【解决方案1】:

如果该值是可选的,则在架构中使用optional: true,如果它为空,它将通过检查。

【讨论】:

    【解决方案2】:

    autoValue 选项由 SimpleSchema 包提供,并记录在那里。 Collection2 为作为 C2 数据库操作的一部分调用的任何 autoValue 函数添加以下属性:

    • isInsert:如果是插入操作则为真
    • isUpdate:如果是更新操作则为真
    • isUpsert: 如果是 upsert 操作则为真(upsert() 或 upsert: true)

    因此,如果您想在更新时提供 autoValue,您必须像这样在架构中使用 isUpdate。

    createdAt: {
        type: Date,
        autoValue: function() {
          if (this.isInsert) {
            return new Date();
          } else if (this.isUpsert) {
            return {$setOnInsert: new Date()};
          } else {
            this.unset();  // Prevent user from supplying their own value
          }
        }
    },
    

    所以你的架构将是这样的:

    previewImage: {
        type: String,
        defaultValue: function() {
             if (this.isInsert) {
                return 'fff';
             } else if (this.isUpdate) {
                return 'fff';
             }
      },
       sourceCode: {
        type: String,
        defaultValue:function() {
             if (this.isInsert) {
                return 'jjj';
             } else if (this.isUpdate) {
                return 'jjj';
             }
      },
      screenShots: {
        type: [String],
        autoValue: function() {
             if (this.isInsert) {
                return [];
             } else if (this.isUpdate) {
                return [];
             }
        }
    },
    

    更多信息请查看this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多