javascript - 将数据从数组上传到 firebase 存储和 Firebase 数据库

标签 javascript firebase react-native firebase-realtime-database firebase-storage

我有一个数组,其中包含一些图像的本地位置,如下所示。

imagePath= ["file:///data/user/0/com.app.taamoutlet/cache/react-native-image-crop-picker/22536474236950.png","file:///data/user/0/com.app.taamoutlet/cache/react-native-image-crop-picker/22583225016770.png "]

我已经完成了单张图片的上传。 根据您在下面看到的代码,我想将图像上传到 firebase 存储。我想将数组中的每个图像上传到一个文件夹。然后收集可下载的 URL 和每个图像,并将该信息存储在您看到的单个产品下

      () => {
        const fs = RNFetchBlob.fs
        const uid = "flyers/" + this.state.productUid;
        const imageRef = firebase.storage().ref(uid).child(Math.random()+".jpg") //string "dp1.jpg"
        let mime = 'image/jpg'

        //------------------------------------------------
        // coverting to base64
        fs.readFile(this.state.imagePath, 'base64')
          .then((data) => {

            //console.log('data='+data);
            return Blob.build(data, { type: `${mime};BASE64` })
          })
          .then((blob) => {
            //uplaoding Image
            uploadBlob = blob
            return imageRef.put(blob, { contentType: mime })
          })
          .then(() => {
            uploadBlob.close()
            //getting url
            return imageRef.getDownloadURL()
          })
          .then((url) => {
            urls = url;
            console.log('urls=' + urls)
            //================================
            try {


              alert("Uploading Flyer" + this.state.title)

              //-------------------------------------------

              //----------Inserting Data to Database--------


              usersTable = 'flyers/' + this.state.productUid,
                console.log(usersTable)
              firebase.database().ref(usersTable).set(
                {

                  title: this.state.title,
                  description: this.state.description,
                  imageUrls: url,
                  storename: this.state.storename,
                  user: asyncValue,
                  longitude: this.state.longitude,
                  latitutde: this.state.latitutde
                }
              ).then(

                alert(this.state.title + " flyer sucessfully uploaded")
              )
              //--------------------------------------------

            }
            catch (error) {
              this.setState({ loading: false })
              this.setState({ disabled: false })
              console.log(error.toString())
              alert(error.toString())
            }
            //================================



          })
      }

最佳答案

如 Renaud 所述,您必须使用 Promise.all。请查看此示例:

const promises = [fs.readFile(this.state.imagePath, 'base64'),...];

return Promise.all(promises).then((arrayOfResults) => {
  const furtherPromises = [...]

  return Promise.all(furtherPromises)
    .then((uploadedFiles) => {
      // action required
  })
    .catch((error) => {
      // action required
    });
  }).catch((error) => {
   // action required
});

关于javascript - 将数据从数组上传到 firebase 存储和 Firebase 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55575297/

相关文章:

reactjs - React Native - StyleSheet.absoluteFill() 和 StyleSheet.absoluteFillObject() 有什么区别?

reactjs - react native : Module RCTLog is not a registered callable module (calling logIfNoNativeHook)

javascript - 性能 : listen to all form elements changes to sync all dynamic attributes with new data

javascript - setTimeout 和 setInterval 可以有重复的 id 吗?

javascript - 在 React.JS 中禁用右键单击

ios - 使用observeSingleEvent/snapshot,如何编写一行代码来检查用户是否在过去24小时内向数据库添加了子条目?

javascript - 如何使用 d3.shape 创建从晚上 9 点开始到早上 6 点结束的半圆

JQuery 对象上的 JavaScript 替换似乎不起作用

android - Firebase 搜索事件术语未显示在分析中

ios - 如何通过 Swift 连接到本地 Firebase 服务器?