【问题标题】:Cannot read proprty uid of null firebase无法读取 null firebase 的属性 uid
【发布时间】: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


    【解决方案1】:

    简单的答案似乎是,您的 currentUser (仍然)为空。这里是官方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
    

    你的家.ts

    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()
                    }));
                });
        }
    }
    

    这样可以确保用户存在。只有这样,这个过程才会开始。否则系统什么也不做。这可能不完全符合您的详细需求,但它应该为您提供找到最终解决方案的正确提示。

    【讨论】:

    • 现在它说Cannot read property 'snapshotChanges' of undefined at Home
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2019-02-05
    • 2019-02-14
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多