这是 NextJS (React) 代码,用于检查集合“users”中是否存在子集合“history” > doc>user-Id,
如果存在,则在历史记录中获取数据,否则保留历史记录 == false。
然后,您可以使用{havehistory?<></>:<></>} 根据数据显示不同的信息。
const [history, setHistory] = useState([])
const [havehistory, setHavehistory] = useState(false)
if(user){
onSnapshot(query(collection(db, "users", user.uid,"history")), (querySnapshot) => {
if(querySnapshot){
const historyBro = querySnapshot.docs.map((doc) => {
return { ...doc.data(), id: doc.id };
});
setHistory(historyBro)
setHavehistory(true)
}
})
}
确保您导入了所需的模块。例如
import { useState } from "react";
import {db} from '../firebase'
import { collection,doc, query, onSnapshot} from "firebase/firestore";
import Link from "next/link";