调用"登录"的传递结果异常:注册



我的应用程序正在使用Meteor + Angular2(此应用程序基于Urigo的教程)(末尾为package.json)

我正在尝试在用户注册后重定向用户。但是我有这个错误:

调用"login"的传递结果异常:signup/http://localhost:3000/app/app.js?hash=5d5262809ea4c507a654caa1a722dfd2ec17a3b9:164:21 userCallback@http://localhost:3000/packages/accounts-password.js?hash=6d4e41828e1dfcc45ff74267779a4e5ecdcd9eda:105:23

我的

用户已成功注册并登录我的应用程序,但未重定向到他的个人资料...我尝试使用和不使用ngZone...我可能误解了什么。

这是我的注册.component.ts注册代码:

signup() {
if (this.signupForm.valid) {
  let user = {
    email: this.signupForm.value.email,
    password: this.signupForm.value.password,
    profile: {
      player: {
        playersName: this.signupForm.value.playersName,
        region: this.signupForm.value.region
      }
    }
  }
  Meteor.call('createUserWithRole', user, function(err, userId) {
    Meteor.loginWithPassword(user.email, user.password, function(err, success){
      this.zone.run(() => {
        if (err) {
          this.error = err;
        } else {
          this.router.navigate(['/my-profile']);
        }
      });
    });
  });
}

}

这是我的服务器/用户/用户.方法.ts

Meteor.methods({
'createUserWithRole': function(data) {
    //console.log(data);
    check(data, {
        email: String,
        password: String,
        profile: {
            player: {
                playersName: String,
                region: String
            }
        }
    });
    let userId;
    userId = Accounts.createUser({
        email: data.email,
        password: data.password,
        profile: {
            newHistory: false,
            player: {
                playerId: null,
                avatar: null,
                playerName: data.profile.player.playersName,
                playercname: data.profile.player.playersName.replace(/s+/g, '').toLowerCase(),
                region: data.profile.player.region,
                revisionDate: null,
                lastManualUpdate: null
            }
        }
    });
    Roles.addUsersToRoles(userId, [
        'active',
        '1man_q',
        '2men_q',
        '3men_q',
        '4men_q',
        '5men_q',
        'global_chat',
        'captain',
        'report',
        'chat_global',
        'chat_lobby',
        'comment',
        'vote',
        'support',
        'default'
    ], 'user_default');
    return data;
},...others methods...skipped...

和我的包.json

{
  "name": "angular2-meteor-base",
  "private": true,
  "scripts": {
    "start": "meteor --settings settings-development.json",
    "start:prod": "meteor run --production",
    "build": "meteor build ./build/",
    "clear": "meteor reset",
    "meteor:update": "meteor update --all-packages",
    "test": "meteor test --driver-package practicalmeteor:mocha",
    "test:ci": "meteor test --once --driver-package dispatch:mocha-phantomjs"
  },
  "devDependencies": {
    "@types/chai": "3.4.34",
    "@types/meteor": "^1.3.31",
    "@types/mocha": "2.2.34",
    "chai": "3.5.0",
    "chai-spies": "0.7.1",
    "meteor-node-stubs": "0.2.4"
  },
  "dependencies": {
    "@angular/common": "2.4.1",
    "@angular/compiler": "2.4.1",
    "@angular/core": "2.4.1",
    "@angular/forms": "2.4.1",
    "@angular/platform-browser": "2.4.1",
    "@angular/platform-browser-dynamic": "2.4.1",
    "@angular/router": "3.4.1",
    "angular2-meteor": "0.7.1",
    "angular2-meteor-accounts-ui": "^1.0.0",
    "angular2-meteor-polyfills": "0.1.1",
    "angular2-meteor-tests-polyfills": "0.0.2",
    "babel-runtime": "^6.18.0",
    "bootstrap": "^4.0.0-alpha.3",
    "meteor-rxjs": "0.4.7",
    "ng2-bootstrap": "^1.3.1",
    "reflect-metadata": "0.1.9",
    "rxjs": "5.0.2",
    "zone.js": "0.7.4"
  }
}

您需要将函数绑定到当前环境。

  Meteor.call('createUserWithRole', user, function(err, userId) {
                Meteor.loginWithPassword(user.email, user.password, function(err, success) {
                    this.zone.run(function(){
                        if (err) {
                            this.error = err;
                        } else {
                            this.router.navigate(['/my-profile']);
                        }
                    }.bind(this));
                }.bind(this));
            }.bind(this));

我认为这可能是由于JavaScript闭包。 在这种情况下,this.router不存在,但我不确定为什么你会得到你当时得到的错误。

试试这个:

Meteor.call('createUserWithRole', user, (err, userId) => {
    Meteor.loginWithPassword(user.email, user.password, (err, success) => {
      this.zone.run(() => {
        if (err) {
          this.error = err;
        } else {
          this.router.navigate(['/my-profile']);
        }
      });
    });
  });

最新更新