简体   繁体   English

如何使用 React.js 从 firebase 获取嵌套文档

[英]How to fetch nested document from firebase using React.js

This is the view of my database:这是我的数据库的视图:

1个

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.

相关问题 如何在 React 应用程序中从 firebase v9 获取单个文档? - How to fetch a single document from a firebase v9 in react app? 如何将这段 firebase 代码转换成它在 React.js 中的最新语法? - How to convert this firebase code into its latest syntax in React.js? 如何从 firebase 获取嵌套的 json 数据? - How to fetch nested json data from firebase? 反应自定义挂钩以从 firebase isLoading 获取文档始终为 false - React custom hook to fetch document from firebase isLoading always false 如何使用查询从 Firebase 9 js 中删除文档? - How do I delete a document from Firebase 9 js using a query? 如何使用 react.js 中的 WHERE 查询计算 Firebase Firestore 中集合中的文档数 - How To Count Number of Documents in a Collection in Firebase Firestore With a WHERE query in react.js 如何在 React.js 中拆分 Firebase Firestore 数组中的数组项值 - How to Split Array Item Values in Firebase Firestore Array in React.js 如何从 Firebase 的集合中的 Firestore 文档中获取单个字段? - How to fetch a single Field from a Firestore document in a collection from Firebase? Firebase react.js 应用已部署 - 空白页 - Firebase react.js app deployed - blank page 如何从 Firebase 获取嵌套数据到回收站视图 - How to fetch nested data from Firebase to recycler view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM