繁体   English   中英

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

[英]AutoValue not set in AutoForm with Meteor method

我有一个使用autoform,collection2和简单模式创建的插入表单。 createdBy字段填充使用autovalue的用户ID。 当使用meteor.allow()进行插入时,该表单有效,但我想用方法替换allow,以便我可以对用户角色进行一些验证,即确保用户具有管理员权限。 但现在我得到一个错误,即createdBy字段为空。

开发工具中的错误是:

错误: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);

该方法(可在客户端和服务器上使用):

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

以及生成表单的模板:

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

您需要通过调用Courses.simpleSchema().clean(course);来清理对象Courses.simpleSchema().clean(course); 在服务器方法中,以便安全地添加自动和默认值。 另外,请注意, this.userIdautoValue功能是null的服务器发起的行动,所以你可能要替换它Meteor.userId()

此外,您必须通过调用Meteor方法中的check(value, pattern)来执行自己的验证,因为可以绕过客户端验证。

例如:

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

所以这有效,但我没有看到它在任何其他例子中使用,所以我有一个不好的感觉,但直到我能找到更多它将必须做:

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