javascript – 如何设置Meteor.userId()以在autoform / simple-schema中使用?

栏目: Node.js · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/25369368/how-do-you-set-the-meteor-userid-for-use-within-autoform-simple-schema

假设我想在用户登录后显示用户购物项目的列表.我正在使用autoform和simple-schema来生成表单元素.当用户第一次登录时,将显示空白购物清单表单.提交表单时,购物清单会保存在数据库中.

我想知道的是如何为每个用户保存这些数据.

通常我会做这样的事情:

ShoppingList.insert({
        name: item_name,
        price: 0,
        createdBy: Meteor.userId()
      });

我将如何使用autoform和simple-schema实现这一目标?是否有可能做这样的事情:

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

翻译自:https://stackoverflow.com/questions/25369368/how-do-you-set-the-meteor-userid-for-use-within-autoform-simple-schema


以上所述就是小编给大家介绍的《javascript – 如何设置Meteor.userId()以在autoform / simple-schema中使用?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithm Design

Algorithm Design

Jon Kleinberg、Éva Tardos / Addison-Wesley / 2005-3-26 / USD 144.20

Algorithm Design introduces algorithms by looking at the real-world problems that motivate them. The book teaches students a range of design and analysis techniques for problems that arise in compu......一起来看看 《Algorithm Design》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

随机密码生成器
随机密码生成器

多种字符组合密码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具