AngularFireList 不可分配给类型 'Observable<Response>

标签 angular firebase firebase-realtime-database ionic3 angularfire2

我有一个 Ionic 页面,它正在查询 FirebaseListObservable 以在 ion-searchbar 上进行动态搜索。它适用于 Angularfire2@4.0.0-rc.0 和 firebase@4.5.0。 升级新版本后AngularFire 5.0我收到有关 FirabaseListObservable 没有从新 Api 导出的问题。

import { Component } from '@angular/core';
import { IonicPage,  NavParams, ViewController } from 'ionic-angular';
import {AngularFireDatabase, FirebaseListObservable} from 'angularfire2/database';
import { Observable} from 'rxjs';
import { Response } from '@angular/http';

@IonicPage()
@Component({
  selector: 'page-modal-groups',
  templateUrl: 'modal-groups.html'
})
export class ModalGroupsPage {

  groups: FirebaseListObservable<any>;

  constructor(public navParams: NavParams,
    public viewCtrl:ViewController,
    public afDatabase: AngularFireDatabase) {
  }

  getItems = (ev: any) : Observable<Response> =>  {
    
    this.groups =  this.afDatabase.list('/Groups', {
       query:{
         orderByChild: 'namelower',
         startAt: (ev.target.value),
         endAt: (ev.target.value + '\uf8ff')
       }
      }
     );


     return this.groups;
  }

  chooseGroups(item:any){
      
    this.viewCtrl.dismiss(item); 
    // console.log(this.product);
  }

  closeModal(){
    this.viewCtrl.dismiss(); 
  }

}
<ion-header>

  <ion-navbar>
    <ion-title>Grup Seç</ion-title>
    <ion-buttons end>
      <button ion-button color="danger" (click)="closeModal()" >
        Kapat
      </button>
    </ion-buttons>
  </ion-navbar>

</ion-header>


  <ion-content padding>
  <ion-searchbar (ionInput)="getItems($event)"></ion-searchbar>
  <ion-list>
    <ion-item  *ngFor="let item of groups | async" (click)="chooseGroups(item)">
      {{ item.name }}
    </ion-item>
  </ion-list>
  <!--<button ion-item  *ngFor="let item of products | async" (click)="chooseProduct(item)">
  {{item.name}}
</button>-->
</ion-content>

然后我用新的 api 更改了查询,但我无法将响应作为可观察的返回,如下所示。我收到这样的错误

** 消息:'Type 'Observable[]>' 不可分配给类型 'Observable'。 类型“AngularFireAction[]”不可分配给类型“Response”。 “AngularFireAction[]”类型中缺少属性“type”。 **

import { Component } from '@angular/core';
import { IonicPage,  NavParams, ViewController } from 'ionic-angular';
import {AngularFireDatabase, AngularFireAction} from 'angularfire2/database';
import { Observable, BehaviorSubject} from 'rxjs';
import { Response } from '@angular/http';
/**
 * Generated class for the ModalGroupsPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-modal-groups',
  templateUrl: 'modal-groups.html',
})
export class ModalGroupsPage {

  

  items: Observable<AngularFireAction<firebase.database.DataSnapshot>[]>;
  group: BehaviorSubject<any>;

  constructor(public navParams: NavParams,
    public viewCtrl:ViewController,
    public afDatabase: AngularFireDatabase) {
  }

  

  getItems = (ev: any) : Observable<Response> =>  {
    this.group = new BehaviorSubject(ev);

    this.items = this.group.switchMap(name =>
    
      this.afDatabase.list('/Groups', ref => name ? ref.orderByChild('namelower').startAt(ev.target.value).endAt(ev.target.value + '\uf8ff') : ref
   
     ).snapshotChanges());
  

     return this.items;
  }

最佳答案

为了得到Observable<Response>5.0 开始的 AngularFireList, 使用 valueChanges()功能。

检查变化 here .

return this.afDatabase.list('/Groups', {
       query:{
         orderByChild: 'namelower',
         startAt: (ev.target.value),
         endAt: (ev.target.value + '\uf8ff')
       }
      }
     ).valueChanges();

如果你想保存this.afDatabase.list()的实例在this.groups , 它将是 AngularFireList 而不是 FirebaseListObservable .

关于AngularFireList 不可分配给类型 'Observable<Response>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679941/

相关文章:

javascript - [Vue 警告] : Error in v-on handler: "TypeError: Object(...)(...).httpsCallable(...).then is not a function"

swift - Xcode/Firebase 改进搜索功能

angular - 找不到要加载的主 socket 'LocalizationListComponent'

css - 具有 flex 模式的 Ngx-datatable 响应行详细信息

angular - 由于内存问题,compodoc 未生成图形

json - 如何在 Angular 2 应用程序中使用 fast-json-patch?

firebase - 无法为 flutter 构建发布 APK

firebase - 参数类型 'Map<String, dynamic> Function()' 不能分配给参数类型 'Map<String, dynamic>'

android - 从 Firebase 获取特定用户的分数和类别

java - 如何禁用向自身发送好友请求的按钮?