Angular firebase auth - 如何从数据库中检索信息(文件网址)?

时间:2018-03-24 10:34:51

标签: javascript angular firebase firebase-realtime-database firebase-authentication

我已经制作了上传服务,允许用户上传头像照片,这只是他们自己看到的,我使用auth uid来定义存储和数据库中的上传路径。上传没有问题,我只是在从数据库中检索该信息时遇到问题。请查看我的代码,下面有更多解释:

upload.service.ts( 我定义上传并获取 ):

正如我所说上传没有问题,现在只有一个问题是 getLitsupload() 。我无法获取文件数据,也无法在页面上显示。我在该函数中得到错误:

  

无法阅读属性' snapshotChanges'未定义的       在UploadService.get [as getListUploads]

import { Injectable } from '@angular/core';
import {
  AngularFireDatabase, AngularFireList, AngularFireAction, DatabaseSnapshot, AngularFireObject,
} from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import * as firebase from 'firebase';
import { Upload } from './upload';
import {AngularFireAuth} from "angularfire2/auth";
import {AuthService} from "../../../core/auth.service";
import {Profile} from "../../../core/profile";

@Injectable()
export class UploadService {
  uploadRef: AngularFireList<any>;
  profile = {} as Profile;
  constructor(private db: AngularFireDatabase,
              public firebaseAuth: AngularFireAuth,
              public auth: AuthService,) {

    //path of uplaoded file data
    this.firebaseAuth.authState.subscribe(auth => {
      this.uploadRef = this.db.list<any>(`profile/${auth && auth.email && auth.uid}/uploads`);
   console.log("Photo uplaod to authuid/profile/uploads")
    })

  }
//getting uploaded files/file (main problem here --> cannot read snapshotChanges() of undefined 
  get getListUploads(): Observable<AngularFireAction<DatabaseSnapshot>[]> {
    return this.uploadRef.snapshotChanges().map(changes => {
      return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
    });
  }

  //upload to storage
  set pushUpload(upload: Upload) {
    const storageRef = firebase.storage().ref();
    this.firebaseAuth.authState.subscribe(auth => {
    const uploadTask = storageRef.child(`uploads/${auth && auth.email && auth.uid}/${upload.file.name}`).put(upload.file);
      uploadTask.on('state_changed',
        (snapshot: firebase.storage.UploadTaskSnapshot) => {
          upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
        }, (error) => {
          console.log(error)
        }, () => {
          upload.url = uploadTask.snapshot.downloadURL
          upload.name = upload.file.name
          this.saveFileData(upload);
        }
      );
    })

  }


  // Save file data (url,name,size etc.) to profile/authuid/uploads

  private saveFileData(upload : Upload): void {
    this.firebaseAuth.authState.subscribe(auth => {
    this.db.object(`profile/${auth && auth.email && auth.uid}/uploads`).set(upload);
    })
  }

}

上传在存储和数据库中均可正常运行:

Database works fine

我做错了什么以及为什么我无法检索文件数据?

奖励:如果还有其他方法可以做,请写一些例子。

编辑:

如果我在没有 auth 的情况下编写所有函数,

一切正常。

这有效:

  constructor(private db: AngularFireDatabase,
              public firebaseAuth: AngularFireAuth,
              public auth: AuthService,) {

    //path of uplaoded file data
      this.uploadRef = this.db.list<any>(`profile`);
   console.log("Photo uplaod to authuid/profile/uploads")

  }

但是我得到的所有照片不仅仅是一个(用户头像):

  get getListUploads(): Observable<AngularFireAction<DatabaseSnapshot>[]> {
    return this.uploadRef.snapshotChanges().map(changes => {
      return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
    });
  }

0 个答案:

没有答案