Firebase存储| Angular 2 - 上传后显示图片的正确方法

时间:2017-10-24 10:30:28

标签: javascript angular firebase-storage

创建活动时,我还会将图片上传到Firebase存储空间。

然后我创建一个数组this.eventz来显示主页上的事件。对于每个我想要检索图像的事件,但是我得到一个Firebse错误:

FirebaseStorageError {code_: "storage/object-not-found", message_: "Firebase Storage: Object 'events/-KxCuUuss1I2uFolk99m/-KxCuUuss1I2uFolk99m_main.png' does not exist.", serverResponse_: "{↵ "error": {↵ "code": 404,↵ "message": "Not Found. Could not get object"↵ }↵}", name_: "FirebaseError"}

据我所知,当我想将图片包含在阵列中时,图片尚未上传,但我不知道如何解决问题。代码工作正常。它显示所有事件,为每个事件添加图像......在创建新事件时,这只是错误。

getEventImage(event) {

if (event) {
            const mainPhotoRef = firebase.storage().ref('events');
            console.log(mainPhotoRef);
            mainPhotoRef.child(`/${event.key}/${event.key}_main.png`).getDownloadURL()
                .then(url => this.makeEvents(url, event))
        }
    }

makeEvents(url, event) {
    console.log(url);
    if (url) {
        try {
            event.img = url;
            let index = _.findIndex(this.eventz, ['key', event.key]);

            if (index >= 0) {
                this.eventz.splice(index, 1);
            }

            this.eventz.push(event);
        } catch (err) {
            console.log(err);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我通过在创建事件对象时将url保存到事件对象来修复我的问题。我想知道我是否正确上传。

private uploadPhoto(itemKey): void {
    this.myPhotosRef.child(itemKey).child(`${itemKey}_main.png`)
      .putString(this.myPhoto, 'base64', { contentType: 'image/png' })
      .then((savedPicture) => this.updateEventImage(savedPicture.downloadURL, itemKey));
  }

  updateEventImage(url, itemKey) {
    console.log(url);
    const newEvent = this.db.object(`events/${itemKey}`)
    newEvent.update({ eventImage: url });
  }