如何为[String]类型集合中的元素设置默认值?
how to set a default value to element in a collection of type [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 }});
}
当我没有给集合中的 "screenShots, previewImage, and sourceCode" 默认值时,提交按钮不起作用。
一旦我给了他们一个默认值,如下所示
previewImage: {
type: String,
defaultValue: "jjj",
},
sourceCode: {
type: String,
defaultValue: "jjj",
},
screenShots: {
type: [String],
autoValue: function() {
return [];
}
},
现在表单中的提交按钮可以使用并且触发了更新方法。它同时更新 "previewImage and sourcCode" 但 "screenShots" 仍然是空的。
我不确定,但我认为问题与 autoValue 有关,我应该将其设置为默认值,但是如何为字符串数组类型的元素提供默认值?
或者问题与其他问题有关?
autoValue 选项由 SimpleSchema 包提供并记录在其中。 Collection2 为作为 C2 数据库操作的一部分调用的任何 autoValue 函数添加以下属性:
- isInsert:如果是插入操作则为真
- isUpdate:如果是更新操作则为真
- isUpsert:如果是更新插入操作则为真(upsert() 或 upsert: true)
因此,如果您想在更新时提供自动值,则必须像这样在您的架构中使用 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
如果该值是可选的,则在架构中使用optional: true
,如果为空,它将通过检查。
单击提交按钮后我有快速表单,此方法被触发
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, and sourceCode" 默认值时,提交按钮不起作用。
一旦我给了他们一个默认值,如下所示
previewImage: {
type: String,
defaultValue: "jjj",
},
sourceCode: {
type: String,
defaultValue: "jjj",
},
screenShots: {
type: [String],
autoValue: function() {
return [];
}
},
现在表单中的提交按钮可以使用并且触发了更新方法。它同时更新 "previewImage and sourcCode" 但 "screenShots" 仍然是空的。
我不确定,但我认为问题与 autoValue 有关,我应该将其设置为默认值,但是如何为字符串数组类型的元素提供默认值?
或者问题与其他问题有关?
autoValue 选项由 SimpleSchema 包提供并记录在其中。 Collection2 为作为 C2 数据库操作的一部分调用的任何 autoValue 函数添加以下属性:
- isInsert:如果是插入操作则为真
- isUpdate:如果是更新操作则为真
- isUpsert:如果是更新插入操作则为真(upsert() 或 upsert: true)
因此,如果您想在更新时提供自动值,则必须像这样在您的架构中使用 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
如果该值是可选的,则在架构中使用optional: true
,如果为空,它将通过检查。