【问题标题】:Code is adding items to list but unable to get any data from db代码正在将项目添加到列表中,但无法从 db 获取任何数据
【发布时间】:2025-12-07 17:25:03
【问题描述】:

我在 Ionic2 中使用 AngularFire2,我试图获取一个列表并使用 ngFor 在视图上显示它,并将新项目推送到 firebase,我的代码正在将项目添加到列表中,但无法从 db 获取任何数据. 以下是服务

constructor(public afd:AngularFireDatabase) {}

getShoppingItems(){

 return this.afd.list('/shoppingItems');

}

在 console.log(Array.from(this.getShoppingItems())) 之后我得到以下结果。

以下是数据库中的数据,我希望得到响应

{"shoppingItems":{"-KwW3BgGsBfd0MeMqeTL":"Rohan","-KwWAK5zrEfwCYM-KdlT":"Vikas"}}

【问题讨论】:

    标签: firebase-realtime-database ionic2 angularfire2


    【解决方案1】:
    //This should be in Provider.ts
    
    getShoppingItems(){
    
     return this.afd.list('/shoppingItems');
    
    }
    
    //In Page.ts, declear shoppingitems as an observable list
    shoppingItems: FirebaseListObservable<any[]>;
    
    
    //Still in Page.ts, inside its constuctor, call the method in provider service
    this.shoppingItems = this.provider_service.getShoppingItems();
    
    //In Page.html, do this
    
    <ion-list *ngFor=let item of shoppingItems | async ></ion-list>
    

    【讨论】:

    • @rohanpadwalkar 你的 page.html 中有什么?
    • 我的page.html
    最近更新 更多