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

查看所有标签

猜你喜欢:

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

Web Development Recipes

Web Development Recipes

Brian P. Hogan、Chris Warren、Mike Weber、Chris Johnson、Aaron Godin / Pragmatic Bookshelf / 2012-1-22 / USD 35.00

You'll see a full spectrum of cutting-edge web development techniques, from UI and eye candy recipes to solutions for data analysis, testing, and web hosting. Make buttons and content stand out with s......一起来看看 《Web Development Recipes》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具