首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新.snapshotChanges()代码以使用angularfire2 5.0.0 rc-8,firebase 5.0.2

更新.snapshotChanges()代码以使用angularfire2 5.0.0 rc-8,firebase 5.0.2
EN

Stack Overflow用户
提问于 2018-05-17 09:30:28
回答 2查看 5.5K关注 0票数 1

我已经成功地更新了angularfire 5.0.0.rc8和firebase 5.0.2,没有错误模块。我想我要做的最后一件事就是修改这个代码来检索所有数据。

代码语言:javascript
复制
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文件..。

代码语言:javascript
复制
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兼容

这是一个新的错误后,我的一半的代码正在工作。插入零件。但我还是无法质疑

运行时错误隐藏(承诺):this.noteListService.getNoteList(...).snapshotChanges(...).map不是TypeError A function TypeError: TypeError不是在createClass (http://localhost:8100/build/vendor.js:10575:20) at createDirectiveInstance (http://localhost:8100/build/vendor.js:10458:20) at createViewNodes (http://localhost:8100/build/vendor.js:11680:36) at createRootView (http://localhost:8100/build/vendor.js:11594:5) at callWithDebugContext (http://localhost:8100/build/vendor.js:12629:25) at Object.debugCreateRootView as createRootView at ComponentFactory_.create (http://localhost:8100/build/vendor.js:12629:25) at ComponentFactory_.create() at (http://localhost:8100/build/vendor.js:49286:44 )处的新HomePage (http://localhost:8100/build/0.js:86:14)的函数)堆栈错误: Uncaught (为了承诺):this.noteListService.getNoteList(...).snapshotChanges(...).map不是一个函数

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-19 05:03:37

新版本angularfire v5.0.0-rc.9每样东西都很好。

这是从firebase检索数据的代码。

代码语言:javascript
复制
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

票数 3
EN

Stack Overflow用户

发布于 2018-05-17 23:04:03

您还没有正确地从rxjs导入map操作符,而且在rxjs 6中还需要使用pipe

代码语言:javascript
复制
...
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()
          }))
        })
      );
  }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50387987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档