繁体   English   中英

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

[英]How do you set the Meteor.userId() for use within autoform/simple-schema?

假设我要在用户登录后显示他们的购物清单。我正在使用自动表单和简单模式来生成表单元素。 当用户首次登录时,将显示空白的购物清单表格。 提交表单时,购物清单将保存在数据库中。

我想知道的是如何为每个用户保存此数据。

通常我会做这样的事情:

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

阅读更多: https : //github.com/aldeed/meteor-collection2/

这为我工作:

creatorID: {
    type: String,
    regEx: SimpleSchema.RegEx.Id,
    autoform: {
        type: "hidden",
        label: false
    },
    autoValue: function () { return Meteor.userId() },
}

this.userId()对我不起作用,而是这样做的:

    autoValue: function() {
        return Meteor.user()._id;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM