2016-03-08 117 views
2

我有quickform一旦提交按钮点击,这种方法被触发如何将默认值设置为类型[String]的集合中的元素?

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 }}); 

    } 

提交按钮不工作时,我并没有给“截屏,previewImage和源代码”集合中默认值。

一旦我给他们的默认值就像如下所示

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

现在的形式提交按钮是否工作正常,更新方法被触发。它会同时更新“previewImage和sourcCode”,但“screenShots”仍然是空的。

我不确定,但我相信问题与autoValue,我应该使它成为一个默认值,但我如何给一个元素类型的字符串数组的默认值?

或者问题与其他事情有关?

+0

我可以知道您用于模式设计的包吗? –

+0

@PankajJatav aldeed/meteor-collection2包如果我明白你的问题对 –

回答

1

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

+0

哦,是啊,没想到这个解决方案,非常感谢 –

1

autoValue选项由SimpleSchema包提供,并在那里记录。 Collection2添加以下属性这对于被称为一个C2数据库操作的一部分的任何autoValue功能:

  • isInsert:如果它是一个插入操作
  • isUpdate:如果它是一个更新操作
  • isUpsert:如果它是一个更新插入操作(无论是UPSERT()或UPSERT:真)

所以,如果你想提供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

相关问题