如何在[String]类型的集合中为元素设置默认值? [英] how to set a default value to element in a collection of type [String]?
问题描述
点击提交按钮后,我便有了快速表单,此方法便被触发
i have quickform once the submit button clicked, this method is fired
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"提供默认值时,提交按钮不起作用.
the submit button wasn't working when i didn't give "screenShots, previewImage, and sourceCode" a default values in a collection.
一旦我给他们一个默认值,如下所示
Once i gave them a default value like it is shown below
previewImage: {
type: String,
defaultValue: "jjj",
},
sourceCode: {
type: String,
defaultValue: "jjj",
},
screenShots: {
type: [String],
autoValue: function() {
return [];
}
},
现在表单中的提交"按钮正在工作,并且触发了更新方法.它会同时更新"previewImage和sourcCode",但"screenShots"仍然为空.
now the submit button in the form is working and the update method is triggered. it updates both "previewImage and sourcCode" but "screenShots" is still empty.
我不确定,但是我认为问题与autoValue有关,我应该将其设为默认值,但是如何为字符串类型数组的元素提供默认值呢?
am not sure but i believe the problem has to do with autoValue which i should make it a default value, but how do i give an element that of type array of string a default value?
还是问题与其他原因有关?
or the problem has to do with something else?
推荐答案
在模式中使用optional: true
,它将通过检查是否为空.
use optional: true
in the schema if the value is optional, and it will pass check if it is empty.
这篇关于如何在[String]类型的集合中为元素设置默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!