AutoValue is not installed in AutoForm with the Meteor method

I have an insertion form that is created using autoform, collection2 and a simple schema. The field is createdBypopulated by userId using autovalue. The form worked when used meteor.allow()for insertion, but I wanted to replace allow with the method so that I could perform some validation of user roles, i.e. Make sure the user has administrator rights. But now I get an error that the field is createdByempty.

Error in dev tools:

error: 400, reason: "Created on demand", details: undefined, message: "Created on demand [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);

Method (which is available on the client and on the server):

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

And the template in which 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>
+4
2

, Courses.simpleSchema().clean(course); , auto default. , this.userId autoValue null , , , Meteor.userId().

, , check(value, pattern) , .

:

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

, , , , , , :

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

Source: https://habr.com/ru/post/1629464/


All Articles