Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70429

Ionic 2 Login With Parse API - Context Problem THIS - returns different class within success and error functions

$
0
0

@amostech wrote:

Hi guys, this is my first post here. I have looked for similar topics but here is an issue I have been trying to get my head around for 2 days.

I have implemented an integration between the Parse API and my first Ionic2 project in order to log users in.
Here is what an authentication call looks like and also the problems.

Attempt 1:

  user = Parse.User.logIn(this.login.username, this.login.password, {
        success: function(user) {
              // Do stuff after successful login.
             this.nav.push(TabsPage); // HERE I WANT TO REDIRECT TO THE LOGGED IN PAGES OF MY APP
        },
       error: function(user, error) { // HERE I WANT TO SHOW AN IONIC 2 ALERT WITH THE ERROR DETAILS
            // The login failed. Check error to see why.
            let alert = Alert.create({
            title: 'Error',
            subTitle: 'Login / Password are incorrect. If you don't have an account you can register through Facebook also.',
            buttons: ['Ok!']
          });
       this.nav.present(alert);
       }
  });

The problem I am having is that inside the success and error functions the (this), which in my case stands for the export class LoginPage (Class representing the LoginPage in Angular2), is an object from a class different than the Angular2 Context. Basically, I can't access any outside elements from the Angular2 exported class within the success and error handlers.

I just don't know why this is happening. I have also tried to ignore the success and error handlers, and using the returned value of the login method from the Parse API, which works (sort of). But their API returns a Promise which is filled with a valid User WHEN the request ends, which makes my app always pop up the error alert message and then logging the user in.

Attempt 2:

  user = Parse.User.logIn(this.login.username, this.login.password, {});
  if(user != null){
    this.nav.push(TabsPage);
  } else {
    let alert = Alert.create({
      title: 'Error',
      subTitle: 'Login / Password are incorrect. If you don't have an account you can register through Facebook also.',
      buttons: ['Ok!']
    });
    this.nav.present(alert);
  }

Has anybody here implemented an integration between Parse and Ionic2 successfully already?

Thanks in advance.
Arthur.

Posts: 4

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 70429

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>