代码之家  ›  专栏  ›  技术社区  ›  Angulandy2

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

  •  1
  • Angulandy2  · 技术社区  · 8 年前

    我提供了上传服务,允许用户上传一张只有自己才能看到的头像照片,我使用auth-uid在存储和数据库中定义上传路径。上传没有问题, 我只是在从数据库中检索这些信息时遇到了问题。请查看我的代码,并提供以下更多解释:

    上载服务ts( 我在其中定义上载和获取 ) :

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

    无法读取未定义的属性“snapshotChanges” 上传服务时。获取[作为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

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

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

    编辑:

    如果我写的所有函数都没有 认证 。

    这项工作:

      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 回复  |  直到 8 年前
    推荐文章