Firebase Storage获取下载URL并将数组内部存储在Firestore中

问题描述 投票:0回答:1

我正在尝试将一组文件(图像)上载到Firebase Storage,然后将其下载URL保存在Firestore中的一个数组内。

uploadImages(name, images) {
    for (let i = 0; i < images.length; i++) {
      const file = images[i].file;
      const path = `${name}/${new Date().getTime()}_${file.name}`;
      this.task = this.storage.upload(path, file);
      let snapshot = this.task.task.snapshot;

      this.task.snapshotChanges().pipe(
        finalize(() => {
          snapshot.ref.getDownloadURL().then(downloadUrl => {
            this.db.collection('test').doc()....

            HOW DO I SAVE ALL DOWNLOAD URLS INSIDE OF AN ASCENDING ARRAY?          

              .then(() => {
                console.log('Successfully uploaded image.');
              })
              .catch(error => {
                throw new Error('Error uploading image:' + error);
              });
          });
        })
      ).subscribe(snap => snapshot = snap);
    }
  }

我的代码为每个图像提供了下载URL,但是如何将它们保存在一个升序数组中?

angular typescript firebase google-cloud-firestore google-cloud-storage
1个回答
0
投票

您可以通过执行类似的操作来正常地将数组保存到Firestore数据库中>

this.db.collection([YOUR_COLLECTION]).add({link: [YOUR_ARRAY]});

其中[YOUR_ARRAY]包含您的下载网址。这样,您将创建一个Firestore文档,其中包含一个名为link的字段,该字段是您的网址数组。该文档将具有自动生成的ID;如果您想给自己一个文档ID,则需要执行以下操作:

this.db.collection([YOUR_COLLECTION]).doc([YOUR_DOCUMENT]).set({link: arr});

关于按升序排列该数组,在添加之前,您需要先对其进行组织。

© www.soinside.com 2019 - 2024. All rights reserved.