Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何设置Meteor.userId()以在autoform/simple架构中使用?_Javascript_Meteor - Fatal编程技术网

Javascript 如何设置Meteor.userId()以在autoform/simple架构中使用?

Javascript 如何设置Meteor.userId()以在autoform/simple架构中使用?,javascript,meteor,Javascript,Meteor,假设我想在用户登录后显示他们的购物项目列表。我使用autoform和简单模式来生成表单元素。当用户第一次登录时,将显示一个空白的购物列表表单。提交表单时,购物清单保存在db中 我想知道的是如何为每个用户保存这些数据 通常我会这样做: ShoppingList.insert({ name: item_name, price: 0, createdBy: Meteor.userId() }); Schema.ShoppingL

假设我想在用户登录后显示他们的购物项目列表。我使用autoform和简单模式来生成表单元素。当用户第一次登录时,将显示一个空白的购物列表表单。提交表单时,购物清单保存在db中

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

通常我会这样做:

      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()
}
});
我如何使用autoform和简单模式实现这一点?是否可以这样做:

      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 }
}
});
阅读更多: 这对我很有效:

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

太好了,谢谢你,我成功了。我也看过文件了。我已经使用quickForm显示了表单。通过将createdBy字段添加到明显显示表单元素的模式中。有什么办法可以隐藏它吗?它不需要显示为将用户ID存储在数据库中。在quickform的文档中,您可以找到:
:可选。绑定数组或指定要包含的以逗号分隔的字段名字符串。只包括列出的字段(及其子字段,如果有),它们将按您指定的顺序显示。
太棒了,谢谢。我用了两个字段,效果很好。我想我会花一些时间来熟悉这些文档!您还可以使用:autoform:{type:“hidden”,label:false},您还可以在提交autoform时调用一个单独的方法,该方法由meteormethod=paramater指定给{{{#autoform}},使用该方法,您可以在保存到Mongo之前执行额外的操作,比如添加createdBy字段。