本文介绍了来自putString()的UploadTask出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having problems with the UploadTask from Firebase Storage.I am trying to upload an image and then saving the image URL on Firestore Cloud. To do so I have this code:

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 时,控制台日志给了我这个错误:

The image is uploaded fine, however when It tries to execute the this.firestore, the console log is giving me this error:

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

I dont know how to get over that error.

编辑

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

This is the import and the constructor in case there is something wrong.

import {  AngularFirestore} from 'angularfire2/firestore';

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

推荐答案

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

You most likely hit a scope issue, which you can resolve by replacing this function:

.then(function (downloadURL)

具有箭头功能(保留了词法范围).

With an arrow-function (which preserves the lexical scope).

.then((downloadURL) =>

这篇关于来自putString()的UploadTask出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:19