简体   繁体   English

无法读取 null 的属性“#”反应

[英]Cannot read property “#” of null react

I am very new to react and I was trying to load profile picture from firebase database.我很陌生,我试图从 firebase 数据库加载个人资料图片。 Even tho the property is not null it keeps saying TypeError: Cannot read property 'pfpUrl' of null.即使该属性不是 null 它一直说 TypeError: Cannot read property 'pfpUrl' of null。

 import React from "react" import { NavLink } from "react-router-dom"; import { useState, useEffect } from "react" import firebase from "firebase"; import fire from "./firebase" import { useHistory } from "react" function Header(){ const [pfp, setPfp] = useState(null) const [userID, setUserID] = useState(null); firebase.auth().onAuthStateChanged((user)=>{ if(user){ setUserID(user.uid) firebase.database().ref("users/"+userID+"/public/profile/pic").on("value", (snapshot)=>{ setPfp(snapshot.val().pfpUrl) }) }else{ } }) function signOut(){ firebase.auth().signOut().then(()=>{ console.log("successfully signout") }) } return( <header> <div id = "hd-container"> <div id = "logo-contain"> <NavLink to = "/"><h1>Logo</h1></NavLink> </div> <div id ="search"> <div className = "bar"> <input id = "searchBar" placeholder = "search"></input> </div> <div className = "searchBtn"> <button id = "srhBtn">Search</button> </div> </div> <div id = "buttons"> <div className = "profile"> <NavLink to = {"users/"+userID}><img src = {pfp} height = "50" width = "50" /></NavLink> </div> <div className = "signout"> <button onClick = {signOut}>Sign Out</button> </div> </div> </div> </header> ) } export default Header;
Even tho the profile pic loads, the error keeps on coming, any fix? 即使加载了个人资料图片,错误仍然不断出现,有什么解决办法吗?

Your use of setUserID(user.uid) will not immediately cause the userID variable to contain the new value.您对setUserID(user.uid)的使用不会立即导致userID变量包含新值。 It will only contain that new value the next time the component renders (not this time it's about to render).它只会在组件一次呈现时包含该新值(而不是这次它即将呈现)。

If you want to use the user's UID in the database query coming up immediately, you will need to use its value locally.如果要在数据库查询中立即使用用户的 UID,则需要在本地使用它的值。

const uid = user.uid
setUserID(uid)
firebase.database().ref("users/"+ uid +"/public/profile/pic").on("value", (snapshot)=>{
    setPfp(snapshot.val().pfpUrl)
})

See that uid is a local variable whose value is able to be used immediately in the query.看到uid是一个局部变量,它的值可以立即在查询中使用。

First you need to execute your code inside useEffect because whenever you are setting state the api will keep on firing the api call.首先,您需要在useEffect中执行您的代码,因为每当您设置 state 时,api 将继续触发 api 调用。 See below code and try display you value in console to verify its coming from backend.请参阅下面的代码并尝试在控制台中显示您的值以验证它来自后端。

useEffect(() => {
    firebase.auth().onAuthStateChanged((user)=>{
            if(user){
                setUserID(user.uid)
                firebase.database().ref("users/"+user.uid+"/public/profile/pic").on("value", (snapshot)=>{
                    setPfp(snapshot.val().pfpUrl)
                })
            }else{
                
            } 
        })
}, [])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM