[英]How do you set the Meteor.userId() for use within autoform/simple-schema?
Say I want to display a list a user's shopping items once they are logged in. I'm using autoform and simple-schema to generate the form elements. 假设我要在用户登录后显示他们的购物清单。我正在使用自动表单和简单模式来生成表单元素。 When the user logs in for the first time a blank shopping list form is displayed. 当用户首次登录时,将显示空白的购物清单表格。 When submitting the form the shopping list is saved in the db. 提交表单时,购物清单将保存在数据库中。
What I want to know is how I can save this data for each user. 我想知道的是如何为每个用户保存此数据。
Normally I would do something like this: 通常我会做这样的事情:
ShoppingList.insert({
name: item_name,
price: 0,
createdBy: Meteor.userId()
});
How would I acheive this using autoform and simple-schema? 我将如何使用自动成型和简单模式来实现这一目标? Would it be possible to do something like this: 是否可以做这样的事情:
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()
}
});
Thanks again :) 再次感谢 :)
If you are using also Collection2 then use autoValue: 如果还使用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 }
}
});
Read more : https://github.com/aldeed/meteor-collection2/ 阅读更多: https : //github.com/aldeed/meteor-collection2/
This worked for me: 这为我工作:
creatorID: {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoform: {
type: "hidden",
label: false
},
autoValue: function () { return Meteor.userId() },
}
this.userId()
didn't work for me, instead this did: this.userId()
对我不起作用,而是这样做的:
autoValue: function() {
return Meteor.user()._id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.