我的目标是获取包含具有相同值的字段的所有文档……第一次运行我的应用程序时,它根据当前ID显示集合中所有文档的所有相似值,代码如下:
const [data, setData] = useState([]);
const [dataSet, setDataSet] = useState({
regNo: "",
});
const {userId} = useParams();
let id = userId
useEffect(() => {
let getUserDoc = async (id) => {
let docRef = doc(db, `dentist/${id}`);
let docSnap = await getDoc(docRef);
console.log('getUserDoc() received:', docSnap);
if (docSnap.exists) {
let theData = docSnap.data();
console.log('getUserDoc() setting userInfo:', theData);
setDataSet(theData);
}
}
if (id) {
getUserDoc(id);
console.log("IdUser", id)
} else {
console.log('useEffect() -- no ID value');
}
}, [id]);
let currentValue = dataSet.regNo
console.log("currentvalue from doc", currentValue)
useEffect(()=>{
const getList = async () => {
const collectionRef = collection(db, "dentist")
const q = query(collectionRef, where("regNo", "==", currentValue))
await getDocs(q).then((task)=>{
let medData = task.docs.map((doc) => ({...doc.data(),
id: doc.id}))
setData(medData)
console.log("medData", medData)
}).catch((err) =>{
console.log(err)
})
}
getList()
}, [])
This worked 100%, I could compare the regNo from the first document against all the documents in the collection, Here's my console log
The challenge I'm facing now is when I refresh the page, the second useEffect returns 0 in the medData, that is, no document.. Here's my console:-
一旦我刷新,我最初看到的数据就消失了!
此外,由于我使用的是Async/AWait,我将所有内容链接在一起,一开始我得到了所有具有类似Regno值的文档,但当我刷新时,它们都消失了.代码是这样的:
const [data, setData] = useState([]);
const [dataSet, setDataSet] = useState({
regNo: "",
});
const {userId} = useParams();
let id = userId
let currentValue = dataSet.regNo
console.log("currentvalue from doc", currentValue)
useEffect(() => {
let getUserDoc = async (id) => {
let docRef = doc(db, `androcare/${id}`);
let docSnap = await getDoc(docRef);
console.log('getUserDoc() received:', docSnap);
if (docSnap.exists) {
let theData = docSnap.data();
console.log('getUserDoc() setting userInfo:', theData);
setDataSet(theData);
}
}
const getList = async () => {
const collectionRef = collection(db, "androcare")
const q = query(collectionRef, where("regNo", "==", currentValue))
await getDocs(q).then((task)=>{
let medData = task.docs.map((doc) => ({...doc.data(),
id: doc.id}))
setData(medData)
console.log("medData", medData)
}).catch((err) =>{
console.log(err)
})
}
if (id) {
getUserDoc(id);
console.log("IdUser", id)
getList();
} else {
console.log('useEffect() -- no ID value');
}
}, [id]);
我观察到的是在刷新时,它将Regno视为空字符串,以及我如何解决此问题.