reactjs Uncaught(in promise)FirebaseError:应为类型“eh”,但它为:a函数

unftdfkk  于 2023-04-29  发布在  React
关注(0)|答案(1)|浏览(103)

我试图在react应用程序中从firestore数据库中获取数据,但它不会显示在页面上。在浏览器控制台中,我得到一个错误,说“Uncaught(in promise)FirebaseError:应为类型“eh”,但它为:a function”这是我的代码:

import { useState, useEffect, useCallback } from "react";
import { db } from "../utils/firebase-config";
import {
  collection,
  getDocs,
} from "firebase/firestore";

export default function MemberPage() {

    const [members, setMembers] = useState([]);
    const membersCollectionRef = useCallback(() => collection(db, "members"),[]);

    useEffect(() => {
            const getMembers = async () => {
            const data = await getDocs(membersCollectionRef);
            setMembers(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
        };
    
        getMembers();
    }, [membersCollectionRef]);

    return (...)}

The database:

xjreopfe

xjreopfe1#

我认为这可能是使用useCallback挂钩的问题
请试试这个

import { useState, useEffect, useCallback } from "react";
import { db } from "../utils/firebase-config";
import {
  collection,
  getDocs,
} from "firebase/firestore";

export default function MemberPage() {

    const [members, setMembers] = useState([]);
    
    const getMembers = async () => {
        const dataSnap = await getDocs(collection(db, "members"));
        setMembers(dataSnap.docs.map((doc) => ({ ...doc.data(), id: doc.id})));
    };

    useEffect(() => {
        getMembers();
    }, []);

    return (...)}

相关问题