All Ionic2 function is not working Inside Parse server function scope

Hi, i am using Parse server for my ionic2 app. It works, but the ionic2 function will not working inside Parse server success function.

if(this.repassword==this.password){
          var parseuser = new Parse.User();
          parseuser.set("username",this.username);
          parseuser.set("password",this.password);
          parseuser.signUp(null, {:slight_smile::slight_smile::slight_smile::slight_smile::slight_smile::slight_smile::slight_smile::slight_smile:Here
              success: function(user) {
                // Hooray! Let them use the app now.
                console.log("Register successful"+user.id);
                console.log(Parse.User.current());
                this.registerSucessAlert();  
                this.c();
              },:slight_smile::slight_smile::slight_smile::slight_smile::slight_smile::slight_smile::slight_smile::slight_smile:
              error: function(user, error) {
                // Show the error message somewhere and let the user try again.
                alert(error);
              }
            });
        }
        else{
          this.registerFailAlert("Password is not the same");
        }

registerSucessAlert(){
      let alert = Alert.create({
        title:'Login',
        subTitle:'Thank you'+this.username+' for register, Press to Continue.',
        buttons:['OK']
      });
      this.nav.present(alert);
  }
c(){
    console.log("HI");
}

the result will be

Register successful Vv10l8AqhE
ParseUser{_objCount:0,className:"_User"โ€ฆblablablablabla
and the alert and c() console will disappear

in the same case, if I rearrange the order.

                this.registerSucessAlert();  
                this.c();
                console.log("Register successful"+user.id);
                console.log(Parse.User.current());

the alert will also not popup and c is also gone. but the Register successful console are also gone. But the register is successful which can be checked on database.

I have check the alert function is totally work fine if you put outside the Parse function scope. But I want to make sure it only fire on the success request.

Dose ionic2 has any method to monitoring variable change or there are some other way to solve it??

Iโ€™ve never used parse before, but what is probably the issue is you have mixed scope.

success: function(user) {

Try

success: (user)=> {
1 Like

Thanks, It works. But I donโ€™t really understand the concept behind this.

โ€œthisโ€ is unreliably weird in JavaScript.

1 Like

https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html

In short, the issue is that youโ€™re using this inside of the function, but in traditional javascript this will change based on the context (which means it wonโ€™t actually call your registerSuccessAlert() function). Using the fat arrow correctly retains this to point to your class, thus actually calling your registerSuccessAlert() function. Youโ€™ll also want to use fat arrows for your error handler too because of this.

1 Like

Oh thats whyโ€ฆ I got it Thanks All