【问题标题】:Retrieving/Handling Image URL From Firestore - React Native从 Firestore 检索/处理图像 URL - React Native
【发布时间】:2020-09-03 19:00:29
【问题描述】:

我是 React Native 和 Firebase (Firestore) 的新手,我正在开发一个应用程序,我必须在其中将帖子检索到我的提要中。

我可以检索我需要的所有数据,但我不知道如何在前端显示图像。 post 文档有一个字段图像,该图像保存为 URL 并存储在 Firebase 存储中。

有谁知道我怎样才能显示图像?我正在使用 a 对数据进行排序。

这是我的retrieveData(),它打印出正确的URL:

  retrieveData() {
    var that = this;
    let postRef = firebase
      .firestore()
      .collection("posts")
      .orderBy("timestamp", "desc")
      .limit(10);
    let allPosts = postRef
      .get()
      .then((snapshot) => {
        snapshot.forEach((doc) => {
          var post = that.state.posts;
          const data = (doc.id, "=>", doc.data());
          that.setState({ posts: post });
          console.log(data.image);
          post.push({
            id: data.id,
            title: data.title,
            description: data.description,
            expireDate: data.expireDate,
            timestamp: data.timestamp,
            image: data.image,
          });
        });
      })
      .catch((err) => {
        console.log("Error getting documents", err);
      });
  }

这就是我在平面列表中调用图像的方式:

<Image
   source={post.image}
   style={styles.postImage}
/>

谁能帮我解决这个问题?

提前致谢。

【问题讨论】:

    标签: firebase react-native google-cloud-firestore react-native-flatlist


    【解决方案1】:

    你能分享图片网址吗?首选方法是将图像存储在 firebase 存储中并获取 downloadUrl,然后将该 url 存储在 firestore 文档中。

    fileref.put(file)
    .then(snapshot => {
       return snapshot.ref.getDownloadURL();   // Will return a promise with the download 
     link
    })
    
    .then(downloadURL => {
      console.log(`Successfully uploaded file and got download link - ${downloadURL}`);
      return downloadURL;
     })
    
    .catch(error => {
      // Use to signal error if something goes wrong.
      console.log(`Failed to upload file and get link - ${error}`);
    });
    

    【讨论】:

    • 您好 Imjaad,感谢您的回复。是的,我将图像存储在 Firebase 存储中并获取 Firestore 文档的 URL。但是当我从文档中检索数据时,它会正确返回控制台上的链接,但不会在前端下载图像!这就是我遇到问题的地方!
    • @JackieMedeiros 你找到解决这个前端问题的方法了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多