显示来自Promise Firebase的数据

时间:2019-08-13 22:21:16

标签: javascript firebase ionic-framework firebase-realtime-database promise

我有这样的服务

Object.defineProperty

此承诺返回此对象数组

enter image description here

在我的组件中,我只是尝试显示头像值:

async getBirthdays() {
    const user = await this.authSP.getUserInfo();
    let bdays = [];
    return await new Promise<any>((resolve) => {
    this.angularFire.database.ref("birthdays").orderByChild("creator_user_id").equalTo(user.uid).once("value", (snapshot) => {
        snapshot.forEach(function (childSnapshot) {
          const value = childSnapshot.val();
          value.$key = childSnapshot.key;
          bdays.push(value);
        });
    });

    resolve(bdays);

    });
}

但是 console.log(data)返回一个空数组。

任何帮助都很重要

2 个答案:

答案 0 :(得分:0)

现在您可以在resolve(bdays)回调运行之前调用once(),这意味着bdays只是一个空数组。

resolve()的调用必须位于bdays的回调中,以包含数据:

async getBirthdays() {
    const user = await this.authSP.getUserInfo();
    let bdays = [];
    return await new Promise<any>((resolve) => {
        this.angularFire.database.ref("birthdays").orderByChild("creator_user_id").equalTo(user.uid).once("value", (snapshot) => {
            snapshot.forEach(function (childSnapshot) {
              const value = childSnapshot.val();
              value.$key = childSnapshot.key;
              bdays.push(value);
            });
            resolve(bdays);
        });    
    });
}

答案 1 :(得分:0)

可以请您尝试以下操作:

this.birthdaySP.getBirthdays().then(res => {
  this.birthdays = res.map(t=>t.avatar);
});