无法在firebase中为聊天应用程序

时间:2017-12-09 08:15:41

标签: angular firebase firebase-realtime-database

我正在尝试使用Angular和Firebase组合聊天应用程序。

我遇到的问题是我无法为注册该应用程序的用户创建单独的父节点。 'users'不会显示为firebase db中的节点。

foll是我的auth.service.ts文件:

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase } from 'angularfire2/database-deprecated';
import * as firebase from 'firebase/app';
import { Observable } from 'rxjs/Observable';
import { User } from '../models/user.model';


@Injectable()
export class AuthService {

  private user: Observable<firebase.User>;
  private authState: any; /*observable to determine the authentication state of a current user*/

  constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase, private router: Router) {

    this.user = afAuth.authState;

   }

   setUserData(email: string, displayName: string, status: string): void {
      const path = `users/${this.currentUserId}`; /*a separate parent node for all of the users in the db*/
      const data = {
        email: email,
        displayName: displayName,
        status: status
      };

      this.db.object(path).update(data)
        .catch(error => console.log(error));
    }

    get currentUserId(): string {
      return this.authState !== null ? this.authState.uid : ''; 
    }

    setUserStatus(status: string): void {
      const path = `users/${this.currentUserId}`;

      const data = {
        status: status
      };

      this.db.object(path).update(data)
        .catch(error => console.log(error));
    }

  /*  authUser() {
      return this.user;
    }*/


   signUp(email: string, password: string, displayName: string) {  /*GATEWAY method*/
      return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
              .then((user) => {
                this.authState = user;
                const status = 'online';
                this.setUserData(email, displayName, status); /*store the user's email, displayname, and status in users node at the time of registration*/
              }).catch(error => console.log(error));
    }


}

此外,我在控制台中看到的错误如下:

TypeError: Cannot read property 'uid' of undefined
    at ChatService.getUser (chat.service.ts:35)
    at SafeSubscriber.eval [as _next] (chat.service.ts:26)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:239)
    at SafeSubscriber.next (Subscriber.js:186)
    at Subscriber._next (Subscriber.js:127)
    at Subscriber.next (Subscriber.js:91)
    at Notification.observe (Notification.js:32)
    at QueueAction.ObserveOnSubscriber.dispatch (observeOn.js:95)
    at QueueAction.AsyncAction._execute (AsyncAction.js:119)
    at QueueAction.execute (QueueAction.js:37)

foll是我的chat.service.ts文件的片段,它试图从firebase中的'users'节点中获取显示名称(未创建)

@Injectable()
export class ChatService {
  user: firebase.User;
  chatMessages: FirebaseListObservable<ChatMessage[]>;
  chatMessage: ChatMessage;
  userName: Observable<string>; 

  constructor(
    private db: AngularFireDatabase,
    private afAuth: AngularFireAuth
    ) {
        this.afAuth.authState.subscribe(auth => {
          if (auth !== undefined && auth !== null) {
            this.user = auth; // the user object is only going to be set if we are authenticated
          }

        this.getUser().subscribe(a => {
            this.userName = a.displayName; 
        });


        });
    }

    getUser() { /*querying for the user id in the users node, which will contain the display name*/
    const userId = this.user.uid;
    const path = `/users/${userId}`;
    return this.db.object(path); /*this will return an observable that we can subscribe to*/
    }

任何指导都将不胜感激。

0 个答案:

没有答案