【发布时间】:2018-11-06 10:17:47
【问题描述】:
我正在尝试将数据存储在 firebase、userId 的列表下。它可以工作,但我收到此错误:typeerror cannot read property 'uid' of null at service.ts,我是新手,需要帮助。
这里使用了 Uid,删除它会使用户 ID 在 firebase 上未定义,而使用它会出现此错误。如果我忽略错误并使用添加项目,它工作得很好,但错误不会消失。
私有类别ListRef = this.db.list(categories-list/${this.currentUser.uid})
service.ts
import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Item } from '../models/item.model';
import * as firebase from 'firebase';
@Injectable()
export class CategoriesListService {
currentUser = firebase.auth().currentUser;
private categoriesListRef = this.db.list<Item>(`categories-list/${this.currentUser.uid}`)
constructor(private db: AngularFireDatabase) {
}
getCategoriesList() {
return this.categoriesListRef;
}
addItem(item: Item) {
return this.categoriesListRef.push(item);
}
editItem(item: Item) {
return this.categoriesListRef.update(item.key, item);
}
removeItem(item: Item) {
return this.categoriesListRef.remove(item.key);
}
}
home.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { CategoriesListService } from '../../services/categories-list.service';
import { Observable } from 'rxjs/Observable';
import { Item } from './../../models/item.model';
@IonicPage()
@Component({
selector: 'page-home',
templateUrl: 'home.html',
})
export class HomePage {
categoriesList$: Observable<Item[]>
constructor(public navCtrl: NavController, public navParams: NavParams, private categories: CategoriesListService) {
this.categoriesList$ = this.categories
.getCategoriesList() //DB List
.snapshotChanges() // Key & Value
.map(changes => {
return changes.map(c => ({
key: c.payload.key,
...c.payload.val()
}));
});
}
ionViewDidLoad() {
console.log('ionViewDidLoad HomePage');
}
}
【问题讨论】:
标签: typescript firebase ionic-framework firebase-authentication angularfire2