简体   繁体   English

AutoForm中没有使用Meteor方法设置AutoValue

[英]AutoValue not set in AutoForm with Meteor method

I have an insert form which is created using autoform, collection2 and simple schema. 我有一个使用autoform,collection2和简单模式创建的插入表单。 The createdBy field is populated with the userId using autovalue. createdBy字段填充使用autovalue的用户ID。 The form worked when using meteor.allow() for the insert but I wanted to replace the allow with a method so that I can do some validating of user roles ie make sure the user has admin rights. 当使用meteor.allow()进行插入时,该表单有效,但我想用方法替换allow,以便我可以对用户角色进行一些验证,即确保用户具有管理员权限。 But now I get an error that the createdBy field is empty. 但现在我得到一个错误,即createdBy字段为空。

The error in dev tools is: 开发工具中的错误是:

error: 400, reason: "Created by is required", details: undefined, message: "Created by is required [400]", errorType: "Meteor.Error"} 错误:400,原因:“创建者是必需的”,详细信息:undefined,message:“Created by is required [400]”,errorType:“Meteor.Error”}

Courses = new Mongo.Collection('Courses');

courseSchema  = new SimpleSchema({
    title: {
        type: String,
        label: "Course Title"
    },
    description: {
        type: String,
        label: "Description"
    },
    createdAt: {
        type: Date,
        autoValue: function(){
            return new Date();
        },
        autoform:{
            type: 'hidden'
        }
    },
    startDate:{
        type: String,
        label: "Start Date"
    },
    sessions: {
        type: String,
        label: "No. of sessions"
    },
    duration: {
        type: String,
        label: "Duration of the course"
    },
    price: {
        type: String,
        label: "Course Price"
    },
    createdBy:{
        type: String,
        autoValue:function(){
            return this.userId;
        },
        autoform:{
            type:'hidden'
        }
    }
});

Courses.attachSchema(courseSchema);

The method (which is available on the client and the server): 该方法(可在客户端和服务器上使用):

Meteor.methods({
    addCourse: function(course){
        Courses.insert(course);
    }
});

And the template where the form is generated: 以及生成表单的模板:

<template name="adminIndex">
   <h1>Available Courses</h1>
   {{> courseList }}    
   <button type="button" class="btn btn-success btn-block">Create New Course</button>
   <h3>Create New Course</h3>
   {{>quickForm id="InsertCourseForm" collection="Courses" type="method" meteormethod="addCourse"}}
</template>

You need to clean the object by calling Courses.simpleSchema().clean(course); 您需要通过调用Courses.simpleSchema().clean(course);来清理对象Courses.simpleSchema().clean(course); in the server method in order to add auto and default values securely. 在服务器方法中,以便安全地添加自动和默认值。 Also, please note that this.userId in your autoValue function is null for server-initiated actions, so you probably want to replace it with Meteor.userId() . 另外,请注意, this.userIdautoValue功能是null的服务器发起的行动,所以你可能要替换它Meteor.userId()

In addition, you must perform your own validation by calling check(value, pattern) in the Meteor method, because client side validation can be bypassed. 此外,您必须通过调用Meteor方法中的check(value, pattern)来执行自己的验证,因为可以绕过客户端验证。

For example: 例如:

if (Meteor.isServer) {
  Meteor.methods({
    addCourse: function(course) {
      Courses.simpleSchema().clean(course);
      check(course, Courses.simpleSchema());
      Courses.insert(course);
    }
  });
}

So this worked but I haven't seen it used in any other examples so I have a bad feeling, but until I can find out more it will have to do: 所以这有效,但我没有看到它在任何其他例子中使用,所以我有一个不好的感觉,但直到我能找到更多它将必须做:

createdBy:{
    type: String,
    autoValue:function(){
        if(Meteor.isClient){
            return this.userId;
        }else if(Meteor.isServer){
            return Meteor.userId(); 
        }
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM