Firebase和离子3应用程序出错

时间:2018-01-27 21:05:04

标签: angular firebase ionic-framework observable

我的离子3和angular5应用程序出现此错误。

键入' Observable'不能分配给' Observable'。输入'任何[]'不能转让给             键入'菜单'。物业'类别'在'任何[]'。

中缺少类型

我试图从FireBase数据库中获取数据。 我的模特:

import { Category } from "./category.model";

export interface Menu {
    key?: string;
    categories: Category[]
}

import { Dish } from "./dish.model";

export interface Category {
    key? : string,
    category: string,
    description: string
    dishes: Dish[],
    imageName: string
}

export interface Dish {
    key?: string,
    imageName: string;
    name: string,
    description: string,
    price: number
}

的DataService:

@Injectable()
export class MenuService {

    private menuRef = this.db.list<Menu>('menu');

    constructor(private db: AngularFireDatabase) {

    }

    getMenu() {
        return this.menuRef;
    }
}

在homepage.ts中,我调用构造函数来获取这样的数据:

constructor(public navCtrl: NavController, public dataService: DataServiceProvider, private menuService: MenuService) {


this.menu = this.menuService.getMenu().snapshotChanges()
.map(
  changes => {
    return changes.map(
      c => ({
        key: c.payload.key, ...c.payload.val()
      }))
  }
);

这些是我的依赖项:

"dependencies": {
    "@angular/common": "5.0.3",
    "@angular/compiler": "5.0.3",
    "@angular/compiler-cli": "5.0.3",
    "@angular/core": "5.0.3",
    "@angular/forms": "5.0.3",
    "@angular/http": "5.0.3",
    "@angular/platform-browser": "5.0.3",
    "@angular/platform-browser-dynamic": "5.0.3",
    "@ionic-native/core": "4.4.0",
    "@ionic-native/splash-screen": "4.4.0",
    "@ionic-native/status-bar": "4.4.0",
    "@ionic/storage": "2.1.3",
    "angularfire2": "^5.0.0-rc.6",
    "firebase": "^4.9.0",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
    "rxjs": "5.5.2",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.18"

问题出在哪里?

1 个答案:

答案 0 :(得分:0)

问题出在这里:

this.db.list<Menu>('menu');

db.list返回一个数组,稍后您将在snapshotChanges()中尝试将其分配给对象。你应该使用:

this.db.Object<Menu>('menu');