迷失在异步功能中。永远得不到解决

时间:2017-08-18 04:25:05

标签: typescript firebase asynchronous firebase-realtime-database

对不起我的英文。我无法理解异步编程!我需要更新变量 userAux2 的用户的节点调用 sToken 。但无论我如何尝试,我总是得到 undefineed 。我已经阅读了每个问题并尝试了许多不同的方法,但我只是不知道如何设置 userAux2 = return snapshot.key; (顺便说一下,快照在控制台中打印好了。)

我真的希望有人可以帮助我,我不知道还能做什么!

谢谢!

  async loginUser(newEmail: string, newPassword: string, Company: string): firebase.Promise<any> {
    var userAux2: string;
    await this.getCurrentUserID(newEmail, Company).then((userAux) => {
      userAux2 = userAux;
    });   

    console.log('userAux2 ==============>>>> ' + userAux2); //this one is undefinned

    await this.updateToken(userAux2, Company).then(() => {
      return this.afAuth.auth.signInWithEmailAndPassword(newEmail, newPassword);   
    }); 
  }

// Get ID of Single User
  async getCurrentUserID(Email: string, Company: string): Promise<any>{
    var ref = this.db.database.ref(`companies/${Company}`);
    ref.child("users")
    .orderByChild("sEmail")
    .equalTo(Email)
    .on("child_added", function(snapshot) {
        console.log('snapshot.key = ' + snapshot.key); 
        return snapshot.key;
    });
 }  

 async updateToken(userAux: string, Company: string){
    var Token1: string;
    Token1 = 'hola';

        var tokenData = {
          sToken: Token1
        };
          console.log('userAux = -------------- ' + userAux);
          var updates = {};
          updates['/companies/' + Company + '/users/' + userAux] = tokenData;

          return this.db.database.ref().update(updates); 
      
  }

0 个答案:

没有答案