我在流星中使用 Autoform 和 Collection2 包。我正在尝试使用插入的数据存储当前登录的用户 ID。这样做的正确方法是什么?
// both/collections/myCollection.js
MyCollection = new Mongo.Collection("mycollection");
MyCollection.attachSchema(new SimpleSchema({
fname : {
type: String,
label: "First Name"
},
lname : {
type: String,
label: "Last Name",
}
}));
MyCollection.allow({
insert: function(userId, doc){
return doc && doc.userId === userId;
},
update: function(userId, doc){
return doc && doc.userId === userId;
}
})
我的模板.html
// client/myTemplate.html
<template name="myTemplate">
{{> quickForm collection="MyCollection" id="insertMyData" type="insert"}}
</template>
我的模板.js
// client/myTemplate.js
Template.myTemplate.created = function(){
var postHooks = {
before: {
insert: function(doc) {
if(Meteor.userId()){
doc.userId = Meteor.userId();
}
return doc;
}
}
}
AutoForm.addHooks('insertMyData', postHooks);
}
我删除了不安全的包并尝试使用 Allow/Deny (链接)写入数据,但现在我收到如下错误:
Meteor.makeErrorType.errorClass {error: 403, reason: "Access denied", details: undefined, message: "Access denied [403]", errorType: "Meteor.Error"…}
通常 Autoform 存储数据,例如:
{
"_id" : "r4uskTttNzADnhZjN",
"fname" : "firstName",
"lname" : "lastName"
}
我想像这样存储:
{
"_id" : "r4uskTttNzADnhZjN",
"fname" : "firstName",
"lname" : "lastName"
"currentUser" : "lsgNynHDrM4Dv9wpH"
}