我有这样的服务
Object.defineProperty
此承诺返回此对象数组
在我的组件中,我只是尝试显示头像值:
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)返回一个空数组。
任何帮助都很重要
答案 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);
});