【问题标题】:submitting form values to meteor method but get error userSchema not defined将表单值提交给流星方法但得到错误 userSchema not defined
【发布时间】:2014-10-10 05:45:45
【问题描述】:

我正在尝试创建一个 Meteor 方法来保存由 autoform 创建的表单中的用户配置文件。我收到一条错误消息,提示未定义 userSchema。它是在 Meteor.isClient 条件内的我的助手中定义的,这可能是问题所在。我尝试让它对客户端和服务器都可用,但没有奏效。

我想要实现的只是获取表单值并将它们插入 Meteor.users.profile。

看起来服务器没有看到客户端上可见的 userSchema。

我收到此错误:

I20140816-14:14:28.170(-7)?调用方法“saveProfile”ReferenceError 时出现异常:未定义 userSchema I20140816-14:14:28.173(-7)?在 Meteor.methods.saveProfile (app/server/server.js:6:16) I20140816-14:14:28.173(-7)?在 MaybeAuditArgumentChecks (packages/livedata/livedata_server.js:1487) I20140816-14:14:28.173(-7)?在包/livedata/livedata_server.js:643 I20140816-14:14:28.173(-7)?在 _.extend.withValue (packages/meteor/dynamics_nodejs.js:56) I20140816-14:14:28.174(-7)?在包/livedata/livedata_server.js:642 I20140816-14:14:28.174(-7)?在 _.extend.withValue (packages/meteor/dynamics_nodejs.js:56) I20140816-14:14:28.176(-7)?在 _.extend.protocol_handlers.method (packages/livedata/livedata_server.js:641) I20140816-14:14:28.176(-7)?在包/livedata/livedata_server.js:541

我的架构:

if (Meteor.isClient) {

Schema = {};

Schema.UserProfile = new SimpleSchema({
    firstName: {
        type: String,
        regEx: /^[a-zA-Z-]{2,25}$/
    },
    lastName: {
        type: String,
        regEx: /^[a-zA-Z]{2,25}$/
    },
    gender: {
        type: String,
        allowedValues: ['Male', 'Female']
    },
    bio: {
        type: String,
    },
    avatar: {
        type: String,
    },
    pinCode: {
        type: Number,
        min: 7,
        max: 7
    },
    phoneNumber: {
        type: Number,
        min: 9,
        max: 10
    }
});



Schema.User = new SimpleSchema({
    _id: {
        type: String,
        regEx: SimpleSchema.RegEx.Id,
        optional: true,
    },
    email: {
        type: String,
        regEx: SimpleSchema.RegEx.Email,
        optional: true,
    },
    createdAt: {
        type: Date,
        optional: true
    },
    profile: {
        type: Schema.UserProfile,
    },
    services: {
        type: Object,
        optional: true,
        blackbox: false
    }
});
SimpleSchema.debug = true;
Meteor.users.attachSchema(Schema.User);

}

这是我的助手:

Template.signupForm.helpers({
  users: function () {
   return Meteor.users;
  },
  userSchema: function () {
   console.log('returning user schema')
   console.log(Schema.User)
     return Schema.User;
  }
});

我的 Meteor 方法(server.js):

Meteor.methods({
  saveProfile: function(doc) {
   console.log('Meteor method 1')
    // Important server-side check for security and data integrity
    check(doc, userSchema);

    console.log('Meteor method')
    console.log(doc.firstName)

  }
});

我的模板

<template name="signupForm">
 <div class="panel-body">
   {{#autoForm schema=userSchema id="signupForm" type="method" meteormethod="saveProfile"}}
  <fieldset>
    {{> afObjectField name='profile'}}
  </fieldset>

  <button type="submit" class="btn btn-primary">Insert</button>
  {{/autoForm}}
</div>
</template>

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    请申请2个步骤:

    1: 将Schema 对象(与所有后代)添加到目录(如文件夹),客户端和服务器都可以看到它。

    2: 在服务器端使用 Schema.User 而不是 userSchema。 请注意,userSchema 是在客户端的 Template.signupForm.helpers 中定义的,因此 Meteor.methods.saveProfile 无法找到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2017-05-21
      相关资源
      最近更新 更多