autoform 创建的登录表单异常(type="normal")

Exception with login form created by autoform (with type="normal")

我尝试使用 meteor 和 autoform 创建简单的登录表单,但出现异常。

这是我生成表单的架构:

FormSchemasLoginUsers = new SimpleSchema({
    email: {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    password: {
        type: String,
        min: 6,
        autoform: {
            type: "password"
        }
    }
});

这是我的表单模板:

<template name="Login">
    {{> quickForm id="loginUserForm" schema="FormSchemasLoginUsers" type="normal" }}
</template>

我尝试用这个钩子处理用户登录:

Template.Login.onRendered(function () {
    AutoForm.addHooks('loginUserForm',
        {
            onSubmit: function (doc) {
                console.log(doc);
                Meteor.loginWithPassword(doc.email, doc.password, function(err) {
                    console.log(err);
                    if (err)
                    {
                        this.done(new Error("Login failed"));
                    }
                    this.done();
                });
                return false;
            },
            onSuccess: function(result) {
                Router.go("home_private");
            },
            onError: function(error) {
                console.log("Error: ", error);
            }
        }
    );
});

但我在 firebug 控制台中收到此错误:

Exception in delivering result of invoking 'login': .onSubmit/<@http://localhost:5000/app/client/views/login/login.js?b8771614cf48d3759cf0d764e51a0693caf23c81:18:5
Meteor.loginWithPassword/<.userCallback@http://localhost:5000/packages/accounts-password.js?8eae27e32c4d1bc1194f7c6dd2aaed1e33a88499:91:21
Ap.callLoginMethod/loginCallbacks<@http://localhost:5000/packages/accounts-base.js?7dabd814506e384c709f8bf707377955f9814129:612:5
_.once/<@http://localhost:5000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:794:14
Ap.callLoginMethod/loggedInAndDataReadyCallback@http://localhost:5000/packages/accounts-base.js?7dabd814506e384c709f8bf707377955f9814129:720:5
Meteor.bindEnvironment/<@http://localhost:5000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:17
._maybeInvokeCallback@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3500:7
.receiveResult@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3520:5
._livedata_result@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:4631:7
Connection/onMessage@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3365:7
._launchConnection/self.socket.onmessage/<@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:2734:11
_.forEach@http://localhost:5000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:149:7
._launchConnection/self.socket.onmessage@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:2733:9
REventTarget.prototype.dispatchEvent@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:173:9
SockJS.prototype._dispatchMessage@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:1158:5
SockJS.prototype._didMessage@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:1216:13
SockJS.websocket/that.ws.onmessage@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:1363:9

meteor....a1802d4 (line 880)

我还使用 Meteor.user() 检查登录是否完成,用户登录是否成功完成。

各位有什么问题吗?? 搞了两天也没找到问题

我认为您收到此问题是因为您在 Meteor.loginWithPassword(user, password, [callback]) which results in a wrong binding of the this value. You could either use ES6 arrow functions 内调用 this.done(); 或只是定义 var self = this; 然后调用 self.done(); 而不是 this.done();

例如:

AutoForm.addHooks('loginUserForm', {
  onSubmit: function(doc) {
    console.log(doc);
    Meteor.loginWithPassword(doc.email, doc.password, (err) => {
      console.log(err);
      if (err) this.done(new Error("Login failed"));
      this.done();
    });
    return false;
  },
  onSuccess: function(result) {
    Router.go("home_private");
  },
  onError: function(error) {
    console.log("Error: ", error);
  }
});