简体   繁体   English

有没有办法用 javascript 调用 firebase API 中的函数?

[英]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 .我在ionic中遇到了javascript的问题,我应该在 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. 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?知道我正在使用 firebase 库并且这种行为是正常的,有什么方法可以避免这个问题吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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