0

My app is using Meteor + Angular2 (This app is based on Urigo's tutorial) (package.json at the end)

I am trying to redirect a user after he signed up. But i have this error :

Exception in delivering result of invoking 'login': signup/http://localhost:3000/app/app.js?hash=5d5262809ea4c507a654caa1a722dfd2ec17a3b9:164:21 userCallback@http://localhost:3000/packages/accounts-password.js?hash=6d4e41828e1dfcc45ff74267779a4e5ecdcd9eda:105:23

My user is successfully registered in my app and logged in, but not redirected to his profile... I tried using with and without the ngZone... I might be misunderstanding something.

This is my signup.component.ts signup code :

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']);
        }
      });
    });
  });
}

}

And this is my server/user/user.methods.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...

and my package.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"
  }
}

2 Answers 2

1

You need to bind your function to the current environnent.

  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));
1
  • 2
    Welcome to SO, Please add more explanation to your answer as the code only answer doesn't tend to help much.
    – Jeet
    Feb 2, 2017 at 16:13
0

I think it might be due to JavaScript closures. this.router woulddn't exist in that case, but I am not sure why you are getting the error you are getting then.

Try this:

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']);
        }
      });
    });
  });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.