如何在meteor中添加自动形成插入数据的额外字段?
我在meteor中使用Autoform和Collection2软件包。我正在尝试使用插入的数据存储当前登录的用户ID。正确的方法是什么如何在meteor中添加自动形成插入数据的额外字段?,meteor,meteorite,meteor-blaze,meteor-autoform,meteor-collection2,Meteor,Meteorite,Meteor Blaze,Meteor Autoform,Meteor Collection2,我在meteor中使用Autoform和Collection2软件包。我正在尝试使用插入的数据存储当前登录的用户ID。正确的方法是什么 // both/collections/myCollection.js MyCollection = new Mongo.Collection("mycollection"); MyCollection.attachSchema(new SimpleSchema({ fname : { type: String, la
// 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;
}
})
myTemplate.html
// client/myTemplate.html
<template name="myTemplate">
{{> quickForm collection="MyCollection" id="insertMyData" type="insert"}}
</template>
我删除了不安全的包,并尝试使用Allow/Deny()写入数据,但现在出现了如下错误:
Meteor.makeErrorType.errorClass {error: 403, reason: "Access denied", details: undefined, message: "Access denied [403]", errorType: "Meteor.Error"…}
通常自动形成存储数据,如:
{
"_id" : "r4uskTttNzADnhZjN",
"fname" : "firstName",
"lname" : "lastName"
}
我想像这样储存:
{
"_id" : "r4uskTttNzADnhZjN",
"fname" : "firstName",
"lname" : "lastName"
"currentUser" : "lsgNynHDrM4Dv9wpH"
}
最好使用允许规则执行此操作。您还可以通过它获得安全性的好处,因为它能够确保字段非常准确
MyCollection.allow({
insert: function(userId, doc){
doc.currentUser = userId;
return true;
},
update: function(userId, doc){
return doc && doc.currentUser === userId;
}
});
要严格解决您的问题,这应该做到:
MyCollection.allow({
insert: function(userId, doc){
return doc && doc.currentUser === userId;
},
update: function(userId, doc){
return doc && doc.currentUser === userId;
}
})
这是合乎逻辑的,因为您不希望向mongo集合注入任何额外属性,因此: 根据官方收集2 您必须添加筛选选项以跳过验证这些额外字段 但是,在插入文档时,这会导致另一个可以理解的问题
"insert failed: Error: undefined is not allowed by the schema"
最后我让它和这个一起工作
MyCollection.insert(task, { validate: false, filter: false });
重要:
请确保在之前调用check方法
这是一个使用Meteor方法进行验证和重定向的完整工作示例:
客户端
AutoForm.addHooks('taskSubmit', {
onSubmit: function (insertDoc, updateDoc, currentDoc) {
Meteor.call('taskInsert', insertDoc, function(error, result) {
if (error) {
Errors.throw(error.reason);
}
Router.go('taskPage', {_id: result._id});
});
return false;
}
});
Tasks = new Mongo.Collection('tasks');
Tasks.attachSchema(taskSchema = new SimpleSchema({
title: {
type: String,
label: "Title"
},
body: {
type: String,
label: "Description",
autoform: {
type: "textarea"
}
}
}
));
Meteor.methods({
taskInsert: function(task) {
check(task, Tasks.simpleSchema());
var user = Meteor.user();
var task = _.extend(task, {
userId: user._id,
author: user.username,
submitted: new Date(),
commentsCount: 0,
progress: 0
});
var id = Tasks.insert(task, { filter: false });
return {
_id: id
};
}
});
服务器端
AutoForm.addHooks('taskSubmit', {
onSubmit: function (insertDoc, updateDoc, currentDoc) {
Meteor.call('taskInsert', insertDoc, function(error, result) {
if (error) {
Errors.throw(error.reason);
}
Router.go('taskPage', {_id: result._id});
});
return false;
}
});
Tasks = new Mongo.Collection('tasks');
Tasks.attachSchema(taskSchema = new SimpleSchema({
title: {
type: String,
label: "Title"
},
body: {
type: String,
label: "Description",
autoform: {
type: "textarea"
}
}
}
));
Meteor.methods({
taskInsert: function(task) {
check(task, Tasks.simpleSchema());
var user = Meteor.user();
var task = _.extend(task, {
userId: user._id,
author: user.username,
submitted: new Date(),
commentsCount: 0,
progress: 0
});
var id = Tasks.insert(task, { filter: false });
return {
_id: id
};
}
});
希望这能帮助某人我尝试过这个东西,但同样的错误出现了“拒绝访问[403]”。我假设“MyCollection.allow({…})”和“var postHooks={…}AutoForm.addHooks(..”)位于正确的位置。它是?