简体   繁体   English

我无法从 FireStore 打印多个文档

[英]I can't print multiple documents from FireStore

I'm able to pull all documents in a collection from Firestore and print each one by one using console.log() command.我可以从 Firestore 中提取集合中的所有文档,并使用 console.log() 命令逐一打印。 But when I want to print these documents to the screen, only the most recent document from the database is printed.但是当我想将这些文档打印到屏幕上时,只会打印数据库中最新的文档。

here is my code.这是我的代码。

import React, {useState, useEffect, createElement} from 'react'
import styled from 'styled-components'
import { db } from '../firebase';
import { collection, query, where, getDocs } from "firebase/firestore";

function Dashboard () {

let [alims, setAlims] = useState([]);
    const getAlim = async() => {
        const colRefBuy = collection(db, "alim");
        const docsSnap = await getDocs(colRefBuy);
        docsSnap.forEach(docBuy => {
            let dataRaw = docBuy.data();
            let datas = dataRaw.name +' : '+ dataRaw.value +'TL, '+ dataRaw.price+' Adet'; 
            setAlims([...alims, datas]);
        })
    } 
useEffect(() => {
        getSatim();
    }, []);


return (
    <Container>
    <BackgroundImage>
        <img src='/images/home-background.png' alt='bg'/>
    </BackgroundImage>
            <Right>
                <h2>Alımlar</h2>
                <div>
                <span>{alims.map((e) => 
                    <span>{e}</span>
                    )}</span>
                </div>
            </Right>
    </Container>
  )
}

but as I said, it only prints the Tesla document, which is the last among 3 documents.但正如我所说,它只打印特斯拉文档,这是 3 个文档中的最后一个。

enter image description here在此处输入图像描述

You should first map an array containing data from all the documents and update the state only once instead of updating it for every document in the loop.您应该首先 map 一个包含来自所有文档的数据的数组,并仅更新 state 一次,而不是为循环中的每个文档更新它。 Try refactoring the code as shown below:尝试重构代码,如下所示:

let [alims, setAlims] = useState([]);

const getAlim = async () => {
  const colRefBuy = collection(db, "alim");
  const docsSnap = await getDocs(colRefBuy);

  const data = docsSnap.docs.map((d) => {
    return dataRaw.name + ' : ' + dataRaw.value + 'TL, ' + dataRaw.price + ' Adet'
  })

  setAlims(data);
}

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

相关问题 我想从 firestore 中的集合中获取多个字段的文档 - I want to get documents for multiple fields from a collection in firestore 无法从 FIRESTORE 的集合中获取多个文档 - UNABLE TO Get multiple documents from a collection FROM FIRESTORE 为 RecyclerView 获取多个 Firestore 文档 - Fetching Multiple Firestore Documents for RecyclerView 如何通过REST API从firestore中的多个文档中删除多个字段? - How to delete multiple fields from multiple documents in firestore through the REST API? 如何在 Cloud Firestore 查询中加入多个文档? - How to join multiple documents in a Cloud Firestore query? 尝试使用用户配置文件中的数组一次从 React 中的 Firestore 查询多个文档 - Trying to query multiple documents from Firestore in React at once, using an array from the users profile 使用来自第 3 方 API 的 python 将多个文档写入 Firestore - Write Multiple Documents to Firestore using python from 3rd party API 如何使用 Angular 5 中的批量写入从 Cloud Firestore 中删除多个文档 - How to delete multiple documents from Cloud Firestore using Batched writes in angular 5 从 Firestore 集合中获取包含所有文档的列表 - Get a list with all documents from a Firestore collection 我无法在 firestore 数据库集合中将列 Kind 更改为 Type - I can't change the column Kind to Type on the firestore database collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM