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

use*_*669 7 javascript meteor

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

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

通常我会做这样的事情:

      ShoppingList.insert({
        name: item_name,
        price: 0,
        createdBy: Meteor.userId()
      });
Run Code Online (Sandbox Code Playgroud)

我将如何使用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()
}
});
Run Code Online (Sandbox Code Playgroud)

再次感谢 :)

Kub*_*bek 20

如果您还使用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 }
}
});
Run Code Online (Sandbox Code Playgroud)

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


Sab*_*ett 5

这对我有用:

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