[英]How to fetch nested document from firebase using React.js
This is the view of my database:这是我的数据库的视图:
import React, { useState } from "react";
import { db } from "../firebase";
import { collection, Firestore, getDocs } from "firebase/firestore";
function Document() {
const handleFetchData = async () => {
//What should I Write to get the documents from Kids collection
};
}
return (
<div>
<button onClick={handleFetchData}> Fetch Data </button>
</div>
);
export default Document;
I want to get the innermost document (document of Kids collection) by just clicking on the button我想通过单击按钮来获取最里面的文档(Kids 集合的文档)
Try once with following code:使用以下代码尝试一次:
const ref = db.collection('Price List').doc('Dry Clean').collection('Kids');
const doc = await ref.get();
var docRef = doc(
db,
'Price List',
'Dry Clean',
'Kids',
'Kids Shirt'
);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
console.log('Document data:', docSnap.data());
} else {
// doc.data() will be undefined in this case
console.log('No such document!');
}
Actually the correct solution is this after firebase 9实际上正确的解决方案是在 firebase 9 之后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.