【问题标题】:TypeError Function expected: App with Angular, TypeScript, Firebase and Ionic预期的 TypeError 函数:带有 Angular、TypeScript、Firebase 和 Ionic 的应用程序
【发布时间】:2019-06-10 21:18:29
【问题描述】:

我研究了带有“TypeError Function expect”的错误,例如herehere,但它们似乎与这个问题无关,我不知道如何解决它。我已经玩了 6 个多小时了。

有人可以帮我吗?

我是一个新手,正在使用 Angular、Firebase、Ionic 3 和 TypeScript 制作购物清单应用程序。它本质上应该将新项目添加到应用程序的显示屏幕/视图,即主页。

下面是接口代码:

export interface Item {
    key?: string;
    name: string;
    quantity: number;
    price: number;

}

下面是HomePage.ts的代码,这里导入了界面Item:

import { Component } from '@angular/core';
import { NavController, IonicPage } from 'ionic-angular';
import { ShoppingListService } from '../../services/shopping list/shopping-list.service';
import { Item } from '../../models/item/item.model';
import { Observable } from 'rxjs/Observable';

@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {

  shoppingList$: Observable<Item[]>

  constructor(public navCtrl: NavController, private shopping: ShoppingListService) {
  this.shoppingList$ = this.shopping
  .getShoppingList() // database list
  .snapshotChanges() // gets key and value
  .map(changes => { // for each change, get a new object
    return changes.map(c => ({
      key: c.payload.key,
      ...c.payload.val()
    }));
  });
  }

以下是应显示项目列表的 homepage.html:

<ion-item *ngFor="let item of shoppingList$ | async" >
    {{item.name}}
   </ion-item>

这是连接到 firebase 数据库的服务提供者代码 shopping-list.service.ts:

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from '@angular/fire/database';
import { Item } from '../../models/item/item.model';

@Injectable()
export class ShoppingListService {
    private shoppingListRef= this.db.list<Item>('shopping-list');

    constructor (private db: AngularFireDatabase) { }
    getShoppingList(){
        return this.shoppingListRef;
    }
}

但我收到此错误:

TypeError: Function expected
   at Anonymous function (http://localhost:8100/build/vendor.js:74479:9)
   at SwitchMapSubscriber.prototype._next (http://localhost:8100/build/vendor.js:62604:13)
   at Subscriber.prototype.next (http://localhost:8100/build/vendor.js:20750:13)
   at Subscriber.prototype._next (http://localhost:8100/build/vendor.js:20786:9)
   at Subscriber.prototype.next (http://localhost:8100/build/vendor.js:20750:13)
   at Subject.prototype.next (http://localhost:8100/build/vendor.js:23237:17)
   at Subscriber.prototype._next (http://localhost:8100/build/vendor.js:20786:9)
   at Subscriber.prototype.next (http://localhost:8100/build/vendor.js:20750:13)
   at Notification.prototype.observe (http://localhost:8100/build/vendor.js:52062:17)
   at DelaySubscriber.dispatch (http://localhost:8100/build/vendor.js:76789:13)

【问题讨论】:

    标签: angular typescript firebase ionic-framework ionic3


    【解决方案1】:

    试试这个:

    首先,导入这个:

    import {of} from "rxjs";
    

    之后,替换这个:

     this.shoppingList$ = of(this.shopping
      .getShoppingList() // database list
      .snapshotChanges() // gets key and value
      .map(changes => { // for each change, get a new object
        return changes.map(c => ({
          key: c.payload.key,
          ...c.payload.val()
        }));
      }));
    

    并重命名服务中的类型:

    private shoppingListRef= this.db.list<Item[]>('shopping-list');
    

    【讨论】:

    • 谢谢。我感谢你的努力。我试过了,它出现了一个新错误:Type 'Observable>' 不可分配给类型 'Observable'。输入 'Observable' 缺少类型“Item[]”的以下属性:length、pop、push、join 等 17 个。
    • 你的变量返回一个项目而不是一个项目数组,我会编辑帖子。
    • 感谢您的帮助。不幸的是,这给出了同样的错误: Type 'Observable' 缺少类型 'Item[]' 的以下属性:length、pop、push、join 和另外 17 个......
    • 它还在服务函数中添加了以下错误:“'Item'类型的参数不可分配给'Item[]'类型的参数。'Item'类型缺少类型中的以下属性'Item[]':长度、pop、push、concat 等等 25 个。”
    猜你喜欢
    • 2021-04-08
    • 2020-01-05
    • 2017-09-16
    • 2015-08-17
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多