无法读取null firebase的proprty uid

时间:2018-05-27 19:47:36

标签: typescript firebase ionic-framework firebase-authentication angularfire2

我试图在firebase中存储数据,userId在列表下。它的工作,但我收到这个错误:typeerror无法读取属性' uid'在service.ts中为null,我是这个需要帮助的新手。

这里使用了Uid,删除它会在使用它时在firebase上未定义用户标识,从而产生此错误。如果我忽略错误并使用添加项目,它的工作正常,但错误不会消失。

private categoriesListRef = 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');
}

}

1 个答案:

答案 0 :(得分:1)

简单的答案似乎是,你的currentUser是(仍)为null。以下是官方docs

您的服务预先假定已登录用户的存在,但在处理用户对象之前不检查此假设是否为真。我估计当你的服务已经尝试获取它时,用户还没有在firebase中完全放置/创建。比赛条件。

currentUser = firebase.auth().currentUser; // no validation whether the user exists.

你最好这样试试。

  

您的服务

@Injectable()
export class CategoriesListService {

currentUser: any;
private categoriesListRef: any;

constructor(private db: AngularFireDatabase) {

    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        this.currentUser = user;
        categoriesListRef = this.db.list<Item>(`categories-list/${this.currentUser.uid}`)
      } else {
        // No user is signed in.
      }
    });

}

...  The rest of your code
  

您的家。

constructor(public navCtrl: NavController, public navParams: NavParams, private categories: CategoriesListService) {
    if(this.categories.getCategoriesList()) {
        this.categoriesList$ = this.categories
            .getCategoriesList() //DB List
            .snapshotChanges() // Key & Value
            .map(changes => {
                return changes.map(c => ({
                    key: c.payload.key,
                    ...c.payload.val()
                }));
            });
    }
}

这样您就可以确保用户存在。然后才开始流程。否则系统什么都不做。这可能不完全适合您的需求,但它应该为您提供正确的提示,以找到您的最终解决方案。