【问题标题】:How can i add extra field with Autoform inserted data in meteor?如何在流星中添加带有 Autoform 插入数据的额外字段?
【发布时间】:2015-07-31 16:16:48
【问题描述】:

我在流星中使用 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;
    }
})

myTemplate.html

// client/myTemplate.html

<template name="myTemplate">
    {{> quickForm collection="MyCollection" id="insertMyData" type="insert"}}
</template>

myTemplate.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 (link) 写入数据,但现在我收到如下错误:

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

【问题讨论】:

    标签: meteor meteorite meteor-blaze meteor-autoform meteor-collection2


    【解决方案1】:

    这是合乎逻辑的,因为您不喜欢向您的 mongo 集合注入任何额外的属性,所以:

    据官方收藏2doc

    您必须添加过滤器选项以跳过验证这些额外字段

    但是这会在插入文档时导致另一个可以理解的问题

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

    希望这对某人有所帮助

    【讨论】:

      【解决方案2】:

      使用允许规则可能会更好。您还可以获得安全性的好处,能够以非常高的确定性确保字段正确。

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

      【讨论】:

      • 我试过这个东西,但同样的错误出现了“访问被拒绝 [403]”。我假设 "MyCollection.allow({...})""var postHooks = {... } AutoForm.addHooks(....)" 是在适当的地方。是吗?
      猜你喜欢
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多