简体   繁体   中英

Is there a way to call functions inside firebase API with javascript?

I'm creating a new thread because I didn't find solutions here.

I'm facing a problem with javascript in ionic in which I should call functions already defined by me inside a firebase API . Here is my situation:

change(val){
 firebase
 .auth()
 .signInWithEmailAndPassword(this.email,val.old_password)
 .then(function(user) {
   firebase
     .auth()
     .currentUser.updatePassword(val.new_password)
     .then(function() {
       console.log("pass changed");
       ### here the function to show the message
     })
     .catch(function(err) {
       console.log("an error happend changing pass");
       ### here the function to show the message
     });
 })
 .catch(function(err) {
   console.log("a very bad error happend");
 });

}

passwordChanged(){
  this.alertCtrl.presentTimeAlert(
    "Password changed",
    "The process was successful",
    "",
    2
  );
}

passError(){
  this.alertCtrl.presentAlert(
    "Error",
    "An error occurred. \n Retry",
    "Okay"
  );
}

My goal is to show a message either if I changed the pass or I didn't. Given the fact that this function is async, I wanted to use passwordChanged and passError as "callback" but they are undefined inside firebase function scope. Actually all my variables and functions are undefined where I should show the message.

Knowing the fact that I'm using firebase library and it can be normal this behavior, there is some way in order to avoid this problem?

Thank you in advanced.

I think you can reach your goal by publishing a message with a event emitter, so you can use the results outside

Here a example

import { Events } from 'ionic-angular';

// first page (publish an event when a user is created)
constructor(public events: Events) {}
createUser(user) {
  console.log('User created!')
  this.events.publish('user:created', user, Date.now());
}


// second page (listen for the user created event after. 
function is called)
    constructor(public events: Events) {
  events.subscribe('user:created', (user, time) => {
    // user and time are the same arguments passed in. 
`events.publish(user, time)`
    console.log('Welcome', user, 'at', time);
  });
}

You can try this approach:

passwordChanged() {
    this.alertCtrl.presentTimeAlert(
      'Password changed',
      'The process was successful',
      '',
      2
    );
  }

  passError(changeErrors) {
    this.alertCtrl.presentAlert(
      'Error',
      'An error occurred. \n Retry',
      'Okay'
    );
  }

  async change(val) {
    const changeErrors = [];

    ///
    // Try to login
    const tryToLogIn = await firebase.auth().signInWithEmailAndPassword(this.email, val.old_password)
      .catch((err) => {
        console.error('A very bad error happend', err);

        changeErrors.push(err);

        return null;
      });

    if (tryToLogIn) {
      ///
      // Try to update the password
      const updatePassword = await new Promise((res) => {
        firebase.auth().currentUser.updatePassword(val.new_password)
          .then(() => { console.log('Password changed'); res(true); })
          .catch((err) => { console.error('An error happend changing pass', err); changeErrors.push(err); res(false); });
      });

      if (updatePassword) {
        // Password was updated
        passwordChanged();
      } else {
        // Password was not updated
        passError(changeErrors);
      }
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM