обновление кода .snapshotChanges () для использования angularfire2 5.0..0 rc-8, firebase 5.0.2 - PullRequest
0 голосов
/ 17 мая 2018

У меня успешное обновление angularfire 5.0.0.rc 8 и firebase 5.0.2 без модуля ошибок.Я думаю, последнее, что мне нужно сделать, это изменить этот код, чтобы получить все данные

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { IonicPage } from 'ionic-angular/navigation/ionic-page';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
import { Note } from '../../model/note/note.model';
import { NoteListService } from '../../services/note-list.service';

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

  noteList: Observable<Note[]>

  constructor(public navCtrl: NavController, private noteListService: NoteListService) {
    this.noteList = this.noteListService.getNoteList()
      .snapshotChanges()
      .map(
        changes => {
          return changes.map(c => ({
            key: c.payload.key, ...c.payload.val()
          }))
        });
  }
}

файл для noteListService ...

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Note } from '../model/note/note.model';

@Injectable()
export class NoteListService {

    private noteListRef = this.db.list<Note>('note-list');

    constructor(private db: AngularFireDatabase) { }

    getNoteList() {
        return this.noteListRef;
    }

    addNote(note: Note) {
        return this.noteListRef.push(note);
    }

    updateNote(note: Note) {
        return this.noteListRef.update(note.key, note);
    }

    removeNote(note: Note) {
        return this.noteListRef.remove(note.key);
    }
}

Помощь по изменению этого кода, который будет совместим смоя новая версия angularfire 5.0.0 rc 8 и firebase 5.0.2

это новая ошибка после того, как половина моего кода работает .. Вставьте часть.Но все же я не могу запросить

Runtime Error
Uncaught (in promise): TypeError: this.noteListService.getNoteList(...).snapshotChanges(...).map is not

функцию TypeError: this.noteListService.getNoteList (...). SnapshotChanges (...). Map не является функцией на новой HomePage(http://localhost:8100/build/0.js:86:14) в createClass (http://localhost:8100/build/vendor.js:10575:20) в createDirectiveInstance (http://localhost:8100/build/vendor.js:10458:20) в createViewNodes (http://localhost:8100/build/vendor.js:11680:36) в createRootView (http://localhost:8100/build/vendor.js:11594:5) в callWithDebugContext (*)1024 * в Object.debugCreateRootView [as createRootView] (http://localhost:8100/build/vendor.js:12116:12) в ComponentFactory_.create (http://localhost:8100/build/vendor.js:9938:29) в ComponentFactoryBoundToModule.create (http://localhost:8100/build/vendor.js:3914:29) в NavControllerBase._viewInit (* 1032)Ошибка стека: Uncaught (в обещании): Ошибка типа: this.noteListService.getNoteList (...). SnapshotChanges (...). Map не является функцией

Ответы [ 2 ]

0 голосов
/ 19 мая 2018

Все отлично работает с новой версией angularfire v 5.0.0-rc.9

Это код для извлечения данных из базы данных.

import { Component, Injectable } from '@angular/core';
/*import { NavController } from 'ionic-angular';*/
import { IonicPage } from 'ionic-angular/navigation/ionic-page';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Note } from '../../model/note/note.model';

@Injectable()
@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  itemsRef: AngularFireList<any>;
  items: Observable<Note[]>;
  constructor(db: AngularFireDatabase) {
    this.itemsRef = db.list('note-list');
    this.items = this.itemsRef.snapshotChanges().pipe(
       map(changes =>
         changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
      )
    );
  }
}

https://github.com/bnayak0225/Biswajit-Note-Ionic-Firebase-angularfire2-5.0.0-rc-9

0 голосов
/ 18 мая 2018

Вы неправильно импортировали оператор карты из rxjs, также как и для rxjs 6, вам нужно использовать pipe.

...
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

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

  noteList: Observable<Note[]>

  constructor(public navCtrl: NavController, private noteListService: NoteListService) {
    this.noteList = this.noteListService.getNoteList()
      .snapshotChanges()
      .pipe(
        map(changes => 
          changes.map(c => ({
            key: c.payload.key, ...c.payload.val()
          }))
        })
      );
  }
}
...