【问题标题】:How To Save Object Of Images To Real-time Firebase如何将图像对象保存到实时 Firebase
【发布时间】:2019-12-05 18:59:59
【问题描述】:

我想将一堆图像保存到 Firebase 存储中,并且它在 Firebase 存储中以“已知图像”方式保存得非常好,所以在保存后我想获取所有 Uri 并将其放入实时数据库作为这样的数组对象

但我在这段代码中尝试过,也像这样保存一张图片!

那么如何处理这些获取Storage中的所有图像,然后将它们放入DB中的数组中呢?

 // Open Gallery
  pickMultiple = () => {
    ImagePicker.openPicker({
      multiple: true
    })
      .then(images => {
        this.setState({
          images: images.map(i => {
            return {
              uri: i.path,
              width: i.width,
              height: i.height,
              mime: i.mime
            };
          })
        });
      })
      .catch(e => console.log(e));
  };
  _SaveImagesToFirebase = () => {
const uid = firebase.auth().currentUser.uid; // Provider
const { images } = this.state;
const provider = firebase.database().ref(`providers/${uid}`);
images.map(image => {
  let file = image.uri;
  const path = "Img_" + Math.floor(Math.random() * 1500 + ".jpg");
  const ref = firebase
    .storage()
    .ref(`provider/${uid}/ProviderGalary/${path}`);
  let imagesArray = [];
  ref
    .put(file)
    .then(() => {
      ref
        .getDownloadURL()
        .then(
          images => {
            console.log(images);
            imagesArray.push({
              uri: images
            });
          },
          error => console.log(error)
        )
        .then(() => {
          provider
            .update({
              Images: imagesArray
            })
            .then(() => console.log("done with imgs"));
        });

      console.log("@inside", imagesArray);
    })
    .then(() => {
      setTimeout(() => {
        this.props.navigation.navigate("Home");
      }, 2000);
    });
  console.log("@OUT", imagesArray);
});
};

【问题讨论】:

    标签: javascript arrays reactjs firebase react-native


    【解决方案1】:

    呃我的错,我只是在 map() 里面定义了 imagesArray 它应该在外面!像这样,

     _SaveImagesToFirebase = () => {
        const uid = firebase.auth().currentUser.uid; // Provider
        const { images } = this.state;
        const provider = firebase.database().ref(`providers/${uid}`);
     => let imagesArray = [];
        images.map(image => {
          let file = image.uri;
          const path = "Img_" + Math.floor(Math.random() * 1500 + ".jpg");
          const ref = firebase
            .storage()
            .ref(`provider/${uid}/ProviderGalary/${path}`);
          ref
            .put(file)
            .then(() => {
              ref
                .getDownloadURL()
                .then(
                  images => {
                    console.log(images);
                    imagesArray.push({
                      uri: images
                    });
                  },
                  error => console.log(error)
                )
                .then(() => {
                  provider
                    .update({
                      Images: imagesArray
                    })
                    .then(() => console.log("done with imgs"));
                });
    
            })
            .then(() => {
              setTimeout(() => {
                this.props.navigation.navigate("Home");
              }, 2000);
            });
        });
      };
    

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 1970-01-01
      • 2021-11-19
      • 2012-02-04
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多