假设我想在用户登录后显示他们的购物项目列表。我正在使用自动表单和简单模式来生成表单元素。当用户第一次登录时,会显示一个空白的购物 list 表单。提交表单时,购物 list 将保存在数据库中。
我想知道的是如何为每个用户保存这些数据。
通常我会这样做:
ShoppingList.insert({
name: item_name,
price: 0,
createdBy: Meteor.userId()
});
我如何使用自动表单和简单模式实现这一点?是否可以做这样的事情:
Schema.ShoppingList = new SimpleSchema({
item: {
type: String,
regEx: /^[a-zA-Z-]{2,25}$/
},
price: {
type: Number,
regEx: /^[0-9]{2,25}$/
},
createdBy: {
type: String,
value: Meteor.userId()
}
});
再次感谢:)
最佳答案
如果您还使用 Collection2,则使用 autoValue:
Schema.ShoppingList = new SimpleSchema({
item: {
type: String,
regEx: /^[a-zA-Z-]{2,25}$/
},
price: {
type: Number,
regEx: /^[0-9]{2,25}$/
},
createdBy: {
type: String,
autoValue:function(){ return this.userId }
}
});
关于javascript - 你如何设置 Meteor.userId() 以在自动表单/简单模式中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25369368/