firebase 未捕获(承诺中)TypeError:无法读取未定义的属性(正在读取"userRole')

pb3s4cty  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(166)

感谢您阅读本文,
我正在尝试引用集合companies/{currentCompany/users/{currentUserUID}中Firebase FireStore中名为userRole的字段
使用用户集合中的公司代码(当我使用console.log时,这一切都有效)
但是,我在第二个useEffect中收到未定义的错误
即使我在请求中添加硬拷贝的数据/代码/值,我仍然会得到相同的错误

const [currentUserUID, setCurrentUserUID] = useState(null)
    const [currentUserCompanyCode, setCurrentUserCompanyCode] = useState(null)
    const [currentUserCompanyRole, setCurrentUserCompanyCodeRole] = useState(null)
        
    useEffect(() => {
      if (currentUserUID != null) {
        const docRefUser = doc(projectDb, "users", currentUserUID);
        getDoc (docRefUser).then((doc) => {
          setCurrentUserCompanyCode(doc.data().companyCode)
        })
      }
    }, [currentUserUID]);
    
    useEffect(() => {
      if (currentUserCompanyCode != null && currentUserUID != null) {
        const docRefCompany = doc(projectDb, "companies", currentUserCompanyCode, "users", currentUserUID);
        getDoc (docRefCompany).then((doc) => {
          setCurrentUserCompanyCodeRole(doc.data().userRole)
        })
      }
    }, [currentUserCompanyCode, currentUserUID]);
    
    console.log(currentUserCompanyCode)
    console.log(currentUserCompanyRole)

bq3bfh9z

bq3bfh9z1#

我已经在Firebase Firestore中重新创建了字段/文档/集合,并修复了该问题。

相关问题