【问题标题】:Problem with the UploadTask from putString()putString() 中的 UploadTask 出现问题
【发布时间】:2019-05-05 19:17:33
【问题描述】:

我在使用 Firebase 存储的 UploadTask 时遇到问题。 我正在尝试上传图像,然后将图像 URL 保存在 Firestore Cloud 上。为此,我有以下代码:

newuploadImage() {
    this.image = 'movie-' + new Date().getTime() + '.jpg';
    let storageRef: any,
    parseUpload: any;
    storageRef = firebase.storage().ref('imgs/' + this.image);
    parseUpload = storageRef.putString(this.cameraImage, 'data_url')
    parseUpload.on('state_changed',
    function (snapshot) {
    }, function (error) {
    }, function () {
        // Upload completed successfully, now we can get the download URL
        parseUpload.snapshot.ref.getDownloadURL().then(function (downloadURL) {
            const idPubli = this.firestore.createId();
            const idReto = this.firestore.createId();
            let IDUser = firebase.auth().currentUser.uid;
            let img = downloadURL
            const idPuntPubli = this.firestore.createId();
            let participacionCreadora = true;
            let punt = 0;
            this.firestore.doc(`public/${idPubli}`).set({
                idPubli,
                idReto,
                IDUser,
                img,
                idPuntPubli,
                participacionCreadora,
                punt,
            })
        });
    })
}

图片上传正常,但是当它尝试执行 this.firestore 时,控制台日志给了我这个错误:

ERROR TypeError: Cannot read property 'firestore' of undefined

我不知道如何克服这个错误。

编辑

这是导入和构造函数,以防出现问题。

import {  AngularFirestore} from 'angularfire2/firestore';

constructor(public camera: Camera, public firestore: AngularFirestore) {
  }

【问题讨论】:

  • 你能分享你的构造函数吗?让我知道你在哪里初始化 firestore?
  • 我已经用导入和构造函数编辑了这个问题。据我所知,它应该是正确的,因为我在其他文件上做了同样的事情并且它一直有效。

标签: typescript firebase ionic-framework google-cloud-firestore firebase-storage


【解决方案1】:

分享我的代码,可能对你有帮助。

  this.imageFile.name.substr(this.imageFile.name.lastIndexOf('.'));
  const fileRef = this.storage.ref(this.filePath);
  this.task = this.storage.upload(this.filePath, this.imageFile);
  this.uploadPercent = this.task.percentageChanges();
  this.task
    .snapshotChanges()
    .pipe(
      finalize(() => {
        this.downloadURL = fileRef.getDownloadURL();
        this.downloadURL.subscribe(url => {
          this.imageUrl = url;
        });
      })
    )
    .subscribe();
  return this.uploadPercent;
}

【讨论】:

  • 您能否添加一些解释此代码如何提供帮助?
【解决方案2】:

您很可能遇到了范围问题,您可以通过替换此函数来解决:

.then(function (downloadURL)

带有箭头函数(保留词法范围)。

.then((downloadURL) =>

【讨论】:

  • 现在我不是在 Firestore 上遇到问题,而是在任何 Firestore 功能上遇到问题。这是现在的错误:ERROR TypeError: Cannot read property 'createId' of undefined
  • 你在哪里初始化this.firestore?它不在您的样本中。也许你还没有初始化它?
  • 我已经编辑了问题,现在它具有构造函数和导入行。直到现在,我一直能够使用数据库。
  • @FernandoSanchezdeNieva - 你已经嵌套了function...,它们都可以转换为箭头函数。即 function (snapshot)function (error)function() 都应该更改以保留您的范围。
  • 我真的不明白你要我做什么?你想让我把所有的,function(snapshot){ },function(error){ }, function () {改成:parseUpload.on('state_changed', (snapshot) =>{})
猜你喜欢
  • 2015-11-14
  • 2011-09-23
  • 2020-08-23
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多