从firestore检索/处理图像url-react js

cpjpxq1n  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(262)

我需要在我的项目前端显示存储在firebase firestore中的图像,但我不知道如何做到这一点。我将在此处提供我的代码:

const {currentUser, } = useAuth();
const [data, setData] = useState([]);

useEffect(() => {
        const fetchData = async () => {
            const data = await firestore.collection("users").doc(currentUser.email).get();
            setData(data.docs.map(doc => ({ ...doc.data(),
            img: doc.data().userImg,})));
        };
        fetchData();
        }, []);
         return (
            <>
             <img src={data.img} alt="Avatar" class="profile" />
            </>
          )

mgdq6dx1

mgdq6dx11#

const {currentUser, } = useAuth();
const [imgUrl, setImgUrl] = useState([]);

useEffect(() => {
        const fetchData = async () => {
            const response = await firebase.firestore().collection("users").doc(currentUser.email).get();
            setImgUrl(response.imgUrl);
        };
        fetchData();
        }, []);
         return (
            <>
             <img src={imgUrl} alt="Avatar" class="profile" />
            </>
          )

获取文档,然后获取imgurl并将值设置为setimgurl状态。将imgurl传递给img标记。

相关问题