Javascript 显示来自promise firebase的数据
我有这样的服务Javascript 显示来自promise firebase的数据,javascript,firebase,ionic-framework,firebase-realtime-database,promise,Javascript,Firebase,Ionic Framework,Firebase Realtime Database,Promise,我有这样的服务 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
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(数据)返回一个空数组
在
once()
回调运行之前,您现在调用resolve(bdays)
将获得任何帮助,这意味着bdays
只是一个空数组
对resolve()
的调用需要在b天的回调中才能包含数据:
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);
});
});
}
async getBirthdays(){
const user=wait this.authSP.getUserInfo();
设b天=[];
返回等待新承诺((解决)=>{
this.angularFire.database.ref(“生日”).orderByChild(“创建者”\u用户id”).equalTo(用户.uid).once(“值”,(快照)=>{
snapshot.forEach(函数(childSnapshot){
const value=childSnapshot.val();
值。$key=childSnapshot.key;
b天推送(值);
});
解决(b天);
});
});
}
现在,在once()
回调运行之前调用resolve(bdays)
,这意味着bdays
只是一个空数组
对resolve()
的调用需要在b天的回调中才能包含数据:
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);
});
});
}
async getBirthdays(){
const user=wait this.authSP.getUserInfo();
设b天=[];
返回等待新承诺((解决)=>{
this.angularFire.database.ref(“生日”).orderByChild(“创建者”\u用户id”).equalTo(用户.uid).once(“值”,(快照)=>{
snapshot.forEach(函数(childSnapshot){
const value=childSnapshot.val();
值。$key=childSnapshot.key;
b天推送(值);
});
解决(b天);
});
});
}
请尝试以下操作:
this.birthdaySP.getBirthdays().then(res => {
this.birthdays = res.map(t=>t.avatar);
});
请您尝试以下方法:
this.birthdaySP.getBirthdays().then(res => {
this.birthdays = res.map(t=>t.avatar);
});
谢谢pb在onceThanks之外有决心。pb的决心超出了一次