简体   繁体   English

React 和 FireStore - 如何找出集合中的文档总数

[英]React and FireStore - How to find out the total number of documents in a collection

In the following code, I use the totalDoclNumbers and a docLimit to create a kind of pagination.在下面的代码中,我使用totalDoclNumbersdocLimit来创建一种分页。 If I assign totalDoclNumbers a static number, everything works.如果我为totalDoclNumbers分配一个 static 编号,一切正常。

But I want that totalDoclNumbers is actually the number of all documents in the collection and it should be calculated programatically.但我希望totalDoclNumbers实际上是集合中所有文档的数量,它应该以编程方式计算。 For this I tried the following:为此,我尝试了以下方法:

import React, { useState, useEffect } from 'react';
import { db } from '../firebase';
import DeleteCard from './DeleteCard';

const List = () => {
  const [cards, setCards] = useState([]);
  const [beginAfter, setBeginAfter] = useState(0);
  const [totalDoclNumbers, setTotalDoclNumbers] = useState(0);

  useEffect(() => {
    const fetchData = async () => {
      const data = await db
        .collection('FlashCards')
        .orderBy('customId')
        .limit(docLimit)
        .startAfter(beginAfter)
        .get();
      setCards(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
      setTotalDoclNumbers(docs.length - 1); // Causes ERROR
      // setTotalDoclNumbers(100); // This works
    };
    fetchData();
  }, [beginAfter]);

  const docLimit = 10;

  // Creating a menu for our documents (10 documents per each menu)
  const RenderDocumentMenu = () =>
    Array(totalDoclNumbers / docLimit)
      .fill()
      .map((_, i) => {
        const onClick = () => setBeginAfter(docLimit * i);
        return (
          <div key={i} className='document__set' onClick={onClick}>
            {docLimit * i + 1} to {docLimit * i + docLimit} Data
          </div>
        );
      });

  return (
    <>
      <div className='document'>
        <RenderDocumentMenu />
      </div>

      <ul className='list'>
        {cards.map((card) => (
          <li key={card.id} className='list__item'>
            <DeleteCard card={card} />
          </li>
        ))}
      </ul>
      <AddCard />
    </>
  );
};

export default List;

But then I get this error: 'docs' is not defined no-undef但后来我得到这个错误:'docs' is not defined no-undef

The line which causes the problem is this one: setTotalDoclNumbers(docs.length - 1);导致问题的行是这一行: setTotalDoclNumbers(docs.length - 1);

What is wrong with my approach?我的方法有什么问题?

It is pretty clear that in your useEffect you need to access the docs using data.docs .很明显,在您的 useEffect 中,您需要使用data.docs访问docs

Like this像这样

...
setTotalDoclNumbers(data.docs.length - 1); 
...

Just a heads up - The data.docs can be an empty array, so in that case, you are doing something like setTotalDoclNumbers(-1) .请注意 - data.docs可以是一个空数组,因此在这种情况下,您正在执行类似setTotalDoclNumbers(-1)的操作。 So just watch out.所以请注意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 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 Native 中的 Firestore 获取集合中的所有文档 - How to get all documents in a collection from Firestore in React Native 有没有办法计算 Firestore 集合中的文档数量? - Is there a way to count number of documents in a collection i Firestore? Firestore:如何通过存在于其他集合中来过滤文档? - Firestore: how to filter documents by existing in other collection? 如何在 firebase firestore 中的集合文档之间循环? - How to loop between documents of a collection in firebase firestore? 如何在 Firestore 中“获取集合中的所有文档”? - How to "Get all documents in a collection" in Firestore? 如何删除firestore集合数据库中的所有文档 - How to delete all the documents in a firestore collection database 如何获取 firestore 集合中的所有文档? - How to get all documents in a collection in firestore? Firestore - 如何确定地从集合中获取多个随机(非重复的、特定数量的)文档? - Firestore - How to get multiple random (non duplicated, specific number of) documents from a collection with certainty? 使用 Firestore 获取文档集合 - get collection of documents with firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM