【问题标题】:Update Profile using React native and firebase使用 React native 和 firebase 更新配置文件
【发布时间】:2021-05-19 20:55:32
【问题描述】:

当我尝试使用 firebase 更新我的用户个人资料时遇到问题。如果按“保存”所有内容都会保存在数据库中,但是当应用程序尝试导航回配置文件时会崩溃。如果我重新加载应用程序,我可以看到我保存的输入显示,所以当我尝试导航时出现问题。

我得到的错误信息是:

undefined 不是一个对象(评估 'props.route.params.uid")

通过使用以下代码:


  const Save = async () => {
      if (imageChanged) {
          const uri = image;
          const childPath = `profile/${firebase.auth().currentUser.uid}`;

          const response = await fetch(uri);
          const blob = await response.blob();

          const task = firebase
              .storage()
              .ref()
              .child(childPath)
              .put(blob);

          const taskProgress = snapshot => {
              console.log(`transferred: ${snapshot.bytesTransferred}`)
          }

          //Saving the Image
          const taskCompleted = () => {
              task.snapshot.ref.getDownloadURL().then((snapshot) => {

                  firebase.firestore().collection("users")
                      .doc(firebase.auth().currentUser.uid)
                      .update({
                         
                          description,
                          image: snapshot,
                      })
                      .then(() => {
                        props.navigation.goBack()
                      })
              })
          }

          const taskError = snapshot => {
              console.log(snapshot)
          }

          task.on("state_changed", taskProgress, taskError, taskCompleted);

    //Save the data if you just save description
      } else {
          saveData({
              description,
          })
      }
  }

  //Save data navigate back to the profile. 
  const saveData = (data) => {
      firebase.firestore().collection("users")
          .doc(firebase.auth().currentUser.uid)
          .update(data).then(() => {
            props.navigation.push("BackToProfile")
        })
  }


所以如果我像这样添加我的 currentUser UID,我会收到以下消息:

“Function CollectionReference.doc() 要求它的第一个参数是非空字符串,但是是:未定义”

  //Save data navigate back to the profile. 
  const saveData = (data) => {
      firebase.firestore().collection("users")
          .doc(firebase.auth().currentUser.uid)
          .update(data).then(() => {
            props.navigation.push("BackToProfile", {uid: currentUser.uid})
        })
  }

我从 Redux 获得的 CurrentUser。我像这样使用 currentUser 显示图像,它工作正常。所以我想如果想要 UID 我应该做同样的事情,还是我想错了?

profileImage={currentUser.image}

当导航完成时。想要我更新我的个人资料图片 和说明。但现在我必须重新加载应用程序。

【问题讨论】:

  • 你能粘贴整个实现吗?
  • 感谢您的回答,我添加了整个实现。

标签: firebase react-native redux


【解决方案1】:

您只是缺少一点代码,这是正确的:

  //Save data navigate back to the profile. 
  const saveData = (data) => {
      firebase.firestore().collection("users")
          .doc(firebase.auth().currentUser.uid)
          .update(data).then(() => {
          //Here the currentUser.uid is undefined so we need to take uid from firebase.auth().currentUser.uid itself.

            props.navigation.push("BackToProfile", {uid: firebase.auth().currentUser.uid})
        })
  }

【讨论】:

  • 您能否详细说明您所做的更改以及更改的原因,以便您的回答真正有用?
  • 你在写 currentUser.uid 但它不可用它在该行未定义,所以我将其更改为 firebase.auth().currentUser.uid
  • 如果对您有帮助,请标记它:)
  • 我的意思是用这些信息更新答案本身,以便它可以帮助每个偶然发现这个问题的人。
  • 感谢您的回答,我在问题中添加了一张图片和更多信息。
猜你喜欢
  • 2019-04-13
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多