首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firebase 'bytesTransferred':属性'bytesTransferred‘不存在于'Object’类型上

Firebase 'bytesTransferred':属性'bytesTransferred‘不存在于'Object’类型上
EN

Stack Overflow用户
提问于 2017-08-04 07:38:24
回答 4查看 4.4K关注 0票数 6

我用的是

  • 角度
  • Firebase

我有什么

  • 允许我上传到firebase存储和实时数据库的上传服务。
  • 这几周来一直运转良好。

The Issue

  • 今天早上不起作用了。
  • 我没有对这个文件或它的组件做任何改变
  • 我唯一改变的是安装了一个消防基础npm来尝试一些其他的胡说八道
  • 这完全停止了我的项目。
  • 我的服务现在显示以下生成时的错误:

'bytesTransferred‘属性在'Object’类型上不存在 类型'Object‘上不存在ts属性'totalBytes’。 类型'() => void‘的参数不能分配给'Unsubscribe’类型的参数。类型'void‘不能被分配到键入’未定义‘。

问题

  • 有人知道为什么会突然显示这个错误消息吗?

我的上传服务

代码语言:javascript
复制
import { Injectable, OnDestroy } from '@angular/core';
import { Upload } from './upload';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import * as firebase from 'firebase';
import { AuthService } from '../../user/auth.service';
import { Subscription } from 'rxjs/Subscription';

// Routes
import { Router, ActivatedRoute } from '@angular/router';

@Injectable()
export class ProjectsAddService implements OnDestroy {
  private _subscription: Subscription;
  private basePath: string = '/site_photo';
  uploads: FirebaseListObservable<Upload[]>;

  constructor(
    private db: AngularFireDatabase, 
    private authService: AuthService, 
     private router: Router
  ) { }






  getUploads(query = {}) {
    this.uploads = this.db.list(this.basePath, {
      query: query
    });
    return this.uploads
  }


  deleteUpload(upload: Upload) {
    this.deleteFileData(upload.$key)
      .then(() => {
        this.deleteFileStorage(upload.name)
      })
      .catch(error => console.log(error))
  }

  // Writes the file details to the realtime db
  private deleteFileData(key: string) {
    return this.db.list(`${this.basePath}/`).remove(key);
  }

  // Firebase files must have unique names in their respective storage dir
  // So the name serves as a unique key
  private deleteFileStorage(name: string) {
    const storageRef = firebase.storage().ref();
    storageRef.child(`${this.basePath}/${name}`).delete()
  }

  ngOnDestroy() {
    if (this._subscription) {
      this._subscription.unsubscribe();
    }
  }

  submitForm(title: string, reference: string, date: string, auditorName: string, newCompanyName: string, upload: Upload) {
    this._subscription = this.authService.user.subscribe(user => {
      if (user) {

        // The User ID of the current user
        var userId = user.uid;

        // Generate a new firebase key to be used for the Project, Project List, Project Members and Storage References
        var newPostKey = firebase.database().ref().child('project_list').push().key;

        // Create a reference to the firebase storage
        const storageRef = firebase.storage().ref();
        const uploadTask = storageRef.child('site_photo/' + user.uid + '/' + newPostKey + '/site_photo').put(upload.file);

        uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
          (snapshot) => {
            // upload in progress
            upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
          },
          (error) => {
            // upload failed
            console.log(error)
          },
          () => {
            // upload success
            upload.url = uploadTask.snapshot.downloadURL
            upload.name = upload.file.name

            // Data to be added to the 'Project' node
            var albumDetails = {
              album_title: title,
              label_name: newCompanyName,
              album_photo: upload.url
            }

            // Data to be added to the 'Project_list' node
            var albumList = {
              label_name: newCompanyName,
              album_photo: upload.url
            }

            // Data to be added to the 'Project_members' node
            var userList = {
              [userId]: true
            }

            // Group the updates to the associated firebase nodes
            var updates = {};
            updates['/album/' + user.uid + '/' + newPostKey] = projectsDetails;
            updates['/album_list/' + user.uid + '/' + newPostKey] = projectsList;
            updates['/project_members/' + newPostKey] = userList;


            // Once the image has been successfully uploaded, make an update to the realtime database
            this.saveToRealtimeDatabase(updates);

            this.router.navigate(['projects']);
          }
        );

      }

    });


  }

  // Perform an atomic Multi-Path update
  private saveToRealtimeDatabase(updates) {
    return firebase.database().ref().update(updates);
  }


}

这里的任何帮助都将是非常感谢的。完全被这件事搞糊涂了。

更新

我注意到,如果我注释掉以下代码,它将正确编译。我只是得到一个错误的阅读进度的上传。

代码语言:javascript
复制
          (snapshot) => {
            // upload in progress
            upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
          },
          (error) => {
            // upload failed
            console.log(error)
          },

第二次更新如下所示,但感觉有点奇怪。任何帮助都将不胜感激。

代码语言:javascript
复制
        var snapshotRef = snapshot as firebase.storage.UploadTaskSnapshot;
        var bytesTransferred = (snapshotRef).bytesTransferred;
        var totalBytes = (snapshotRef).totalBytes;
        upload.progress = (bytesTransferred / totalBytes) * 100;

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-08-06 21:40:48

用下面的代码替换您的代码片段。

代码语言:javascript
复制
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
  (snapshot) =>  {
    upload.progress = (uploadTask.snapshot.bytesTransferred / uploadTask.snapshot.totalBytes) * 100;
    console.log(upload.progress);
  },
票数 14
EN

Stack Overflow用户

发布于 2017-08-04 09:42:37

我对打字稿或这个包还不太了解,无法确切地解释到底发生了什么,但至少我可以修复它。

显然,新版本中的完整回调不再接受任何返回类型,而只接受未定义的类型。我认为这与返回未定义的默认退订有关。在您的代码中,您可以通过手动返回回调中未定义的内容来修复错误。

代码语言:javascript
复制
() => {
    console.log('upload complete');
    return undefined;
}

类型记录显然不能推断第一个回调中快照的类型,您还需要手动指定它,可以使用类型广播,也可以从回调中调用单独的函数。

代码语言:javascript
复制
 console.log((snapshot as firebase.storage.UploadTaskSnapshot).bytesTransferred);

如果有人能给我更深入的解释,我会很感激的。

票数 2
EN

Stack Overflow用户

发布于 2018-06-26 14:58:45

也有同样的问题。通过设置快照的类型来修正。这是打字稿。

代码语言:javascript
复制
uploadTask.on('state_changed', (snapshot: firebase.storage.UploadTaskSnapshot) => {
    upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45500854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档