Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未使用Meteor方法在AutoForm中设置AutoValue_Javascript_Meteor_Meteor Autoform_Meteor Collection2_Meteor Methods - Fatal编程技术网

Javascript 未使用Meteor方法在AutoForm中设置AutoValue

Javascript 未使用Meteor方法在AutoForm中设置AutoValue,javascript,meteor,meteor-autoform,meteor-collection2,meteor-methods,Javascript,Meteor,Meteor Autoform,Meteor Collection2,Meteor Methods,我有一个使用autoform、collection2和简单模式创建的插入表单。createdBy字段使用autovalue填充用户ID。表单在为插入使用meteor.allow()时起作用,但我想用一个方法替换allow,以便我可以对用户角色进行一些验证,即确保用户具有管理员权限。但是现在我得到一个错误,createdBy字段为空 开发工具中的错误是: 错误:400,原因:“需要创建人”,详细信息:未定义, 消息:“创建人是必需的[400]”,错误类型:“Meteor.Error”} 方法(在客

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

开发工具中的错误是:

错误:400,原因:“需要创建人”,详细信息:未定义, 消息:“创建人是必需的[400]”,错误类型:“Meteor.Error”}

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

以及生成表单的模板:

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

现有课程
{{>课程列表}
创建新课程
创建新课程
{{>quickForm id=“InsertCourseForm”collection=“Courses”type=“meteormethod=“addCourse”}

您需要通过调用
Courses.simpleSchema().clean(course)来清理对象,以便安全地添加自动值和默认值。另外,请注意,对于服务器启动的操作,
autoValue
函数中的
this.userId
null
,因此您可能希望将其替换为
Meteor.userId()

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

例如:

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

感谢Matthias,对于乐观用户界面来说,客户端和服务器可以使用代码的问题是有道理的,但我对this.userId和Meteor.userId很好奇。我不确定代码实际在哪里运行,我猜是客户端还是服务器,或者两者都在运行。我假设方法存根在更新minimingo缓存之前在本地进行验证,然后在更新mongo db之前在服务器上进行验证。自动值实际运行在哪里?我在客户机上进行了假设,然后将详细信息“发布”(以HTTP方式)到服务器上。这是否准确?架构将在客户端和服务器上进行验证。您可以通过添加
console.log(this.userId)来验证这一点
在您的
autoValue
函数中,您将在浏览器控制台中看到用户ID,并在服务器控制台中看到
null
。这是有道理的,但我现在对autoValue的功能非常困惑。我原以为它将填充表单元素,然后用户ID将作为表单数据“发布”到服务器。否则,使用autoValue有什么意义。为什么不在将数据插入数据库之前,将userId添加到方法中接收的数据中呢?我将阅读quickform。以下是文档摘录:
autoValue
函数仅在客户端上运行以进行验证,但保存的实际值将始终在服务器上生成,无论插入/更新是从客户端还是从服务器启动。嗨,Matthias,clean是不必要的,因为它是由集合2在内部自动调用的。userId()就是我一直在寻找的东西。谢谢你的帮助。这完全没有必要,上面马提亚的回答起作用了。
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(); 
        }
    },