Bis*_*jit 1 firebase angularfire2 ionic3 angular
我已成功更新 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()
}))
});
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
帮助更改此代码,使其与我的新版本 angularfire 5.0.0 rc 8 和 firebase 5.0.2 兼容
这是我的一半代码工作后的新错误.. 插入部分。但我仍然无法查询
Run Code Online (Sandbox Code Playgroud)Runtime Error Uncaught (in promise): TypeError: this.noteListService.getNoteList(...).snapshotChanges(...).map is not函数 TypeError: this.noteListService.getNoteList(...).snapshotChanges(...).map 不是新主页 ( 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 ( http://localhost:8100/build/vendor.js: 12629:25 ) 在 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 ( http:// localhost:8100/build/vendor.js:49286:44 ) 堆栈错误:未捕获(承诺):类型错误:this.noteListService.getNoteList(...).snapshotChanges(...).map 不是函数
您没有正确地从 rxjs 导入 map 操作符,同样从 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()
}))
})
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5388 次 |
| 最近记录: |