有没有办法使用JavaScript在Firebase API中调用函数?

时间:2019-10-27 11:24:59

标签: javascript firebase ionic-framework

我正在创建一个新线程,因为我在这里找不到解决方案。

我在 ionic 中的 javascript 遇到问题,我应该在 firebase API 中调用我已经定义的函数。这是我的情况:

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"
  );
}

我的目标是在我更改密码或未更改密码时显示一条消息。考虑到该函数是异步的,我想使用 passwordChanged passError 作为“回调”,但是它们在firebase函数范围内未定义。实际上,我所有的变量和函数都是 undefined ,我应该在其中显示消息。

知道我正在使用Firebase库这一事实,并且这种行为可能是正常的,有某种方法可以避免此问题?

谢谢你。

2 个答案:

答案 0 :(得分:0)

我认为您可以通过使用事件发射器发布消息来达到目标​​,因此可以在外部使用结果

这里有个例子

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

答案 1 :(得分:0)

您可以尝试这种方法:

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