Javascript 简单模式/自动表单集合中的用户特定字段插入[Meteor]
我使用一个简单的模式和一个快速表单来允许用户将文档持久化到我的集合中,但我想添加Javascript 简单模式/自动表单集合中的用户特定字段插入[Meteor],javascript,mongodb,meteor,meteorite,Javascript,Mongodb,Meteor,Meteorite,我使用一个简单的模式和一个快速表单来允许用户将文档持久化到我的集合中,但我想添加Meteor.userId()和Meteor.user().username字段,最终只返回特定于当前用户的数据。因为quickForms没有显示它的insert()方法,所以我不确定在哪里添加字段 下面是我用来允许用户将演讲者持久化到集合的模式 Speakers = new Meteor.Collection('speakers'); Speakers.attachSchema(new Simpl
Meteor.userId()
和Meteor.user().username
字段,最终只返回特定于当前用户的数据。因为quickForms没有显示它的insert()
方法,所以我不确定在哪里添加字段
下面是我用来允许用户将演讲者
持久化到集合的模式
Speakers = new Meteor.Collection('speakers');
Speakers.attachSchema(new SimpleSchema({
first: {
type: String,
label: "first name",
max: 200
},
last: {
type: String,
label: "last name",
max: 200
},
date: {
type: Date,
label: 'date',
optional: true
}
}));
我不确定我是否正确理解你,但为什么不呢
Speakers.attachSchema(new SimpleSchema({
first: {
type: String,
label: "first name",
max: 200
},
last: {
type: String,
label: "last name",
max: 200
},
date: {
type: Date,
label: 'date',
optional: true
},
userId: {
type: String
}
}));
如果要根据插入记录的用户自动填充,请使用
要使用quickForm而不显示userId字段,可以执行以下操作
{{> quickForm collection="Speakers" id="speakerForm" type="insert" omitFields="userId"}}
如果您想从输入表单中省略一些字段并自己用值填充这些字段,可以使用带有before钩子的afQuickField 首先,将userId和username字段添加到模式中
{{#autoForm collection=speakerCollection id="speakerForm" type="insert"}}
{{> afQuickField name="first"}}
{{> afQuickField name="last"}}
{{> afQuickField name="date"}}
<div>
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-default">Reset</button>
</div>
{{/autoForm}}
然后添加一个before钩子来添加用户ID:
AutoForm.hooks({
speakerForm: {
before: {
"insert": function (doc) {
doc.userId = Meteor.userId();
return doc;
},
}
});
您可以使用
autoValue
定义自动插入的值:
Speakers = new Meteor.Collection('speakers');
Speakers.attachSchema(new SimpleSchema({
userId: {
type: String,
autoValue: function() {
return Meteor.user()._id;
}
},
author: {
type: String,
autoValue: function() {
return Meteor.user().username;
}
},
}));
如果要省略自动表单中的某些字段,可以设置autoform.omit=true
:
userId: {
type: String,
autoValue: function() {
return Meteor.user()._id;
},
// omit the userId field in autoform forms
autoform: {
omit: true
}
}
您正确地理解了这个问题——autovalue是我的模式的帮助器吗?如果我将
userId
字段添加到我的quickForm模式中,userId
然后在我的表单提交中显示为一个字段。一般来说,quickForm非常适合入门,但是如果您想要更大的灵活性,您可以使用单独的方法。quickForm仍然可以通过“忽略字段”属性使用。
userId: {
type: String,
autoValue: function() {
return Meteor.user()._id;
},
// omit the userId field in autoform forms
autoform: {
omit: true
}
}