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中使用?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Mastering Regular Expressions, Second Edition

Mastering Regular Expressions, Second Edition

Jeffrey E F Friedl / O'Reilly Media / 2002-07-15 / USD 39.95

Regular expressions are an extremely powerful tool for manipulating text and data. They have spread like wildfire in recent years, now offered as standard features in Perl, Java, VB.NET and C# (and an......一起来看看 《Mastering Regular Expressions, Second Edition》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具