簡體   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