简体   繁体   English

无法在反应中呈现来自 firebase 的收集数据

[英]Unable to render collection data from firebase in react

I'm trying to retrieve a collection from firebase and then use a map to loop through the docs and render some UI elements, please see the code below, I can see the correct data in the console.log at line 20 but the map function does not appear to be working.我正在尝试从 firebase 检索集合,然后使用地图循环浏览文档并呈现一些 UI 元素,请参阅下面的代码,我可以在第 20 行的 console.log 中看到正确的数据,但地图功能似乎没有工作。

 function Candidates() {

const [visibleProfiles, setVisibleProfiles] = useState([])


useEffect(()=>{

    const getProfiles = firestore.collection("visible-profiles")
    .onSnapshot(snapshot => {
        const list = snapshot.docs.map((doc) => {
            return {id: doc.id, ...doc.data()}
        })

        setVisibleProfiles(list)
        console.log(list)
    })
    
    return () => getProfiles()

    return visibleProfiles

}, [])




return (
<>
<h1>CANDIDATES</h1>
    
        { visibleProfiles.map((obj, key) => {
            <>
            <p>{obj.location}</p>
            <CandidateCard
            firstname = {obj.firstname}
            lastname = {obj.lastname}
            title = {obj.title}
            profileImgUrl = {obj.profileImgUrl}
            location = {obj.location}
            />
            </>
        })}
    
</>    
);

Try with optional chaining and you have to return JSX尝试使用可选链接,你必须返回 JSX

return (
  <>
    <h1>CANDIDATES</h1>
    {visibleProfiles && visibleProfiles.map((obj, key) => {
        return(<>
          <p>{obj.location}</p>
          <CandidateCard
            firstname = {obj.firstname}
            lastname = {obj.lastname}
            title = {obj.title}
            profileImgUrl = {obj.profileImgUrl}
            location = {obj.location}
          />
        </>);
     })}
  </>    
);

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

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