首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Firestore数据库检索数据

如何从Firestore数据库检索数据
EN

Stack Overflow用户
提问于 2021-08-26 02:21:57
回答 1查看 59关注 0票数 0

我正在做一个React项目,在输入一个值然后单击search按钮后,应用程序搜索数据库中是否存在id。如果是,则在同一页中显示搜索结果。我很难分配搜索的值,然后显示它。当我试图将搜索结果分配给一个数组时,它会给出一个错误:

代码语言:javascript
复制
Type 'DocumentData[]' is not assignable to type 'Dispatch<SetStateAction<Identification[]>>'.
Type 'DocumentData[]' provides no match for the signature '(value:SetStateAction<Identification[]>): void'.

当我只对没有变量的数据执行console.log时,我可以得到结果,但是我需要它在setId变量中。

以下是代码:

代码语言:javascript
复制
import React, {ChangeEvent} from "react";
import { useState,useEffect } from "react";
import LongText from "../atoms/LongText";
import AppListBI from "./AppListBI";
import {Identification} from "../../assets/Person/Person";
import db from "../../firebase.config"

const Core = () => {
var [input, setInput] = useState('')
const [showResults, setShowResults] = React.useState(false)
var [person, setId] =  useState<Identification[]>([]);

const fetchBI =  async () => {
const ref=db.collection('id').where('numberId','==',input).get().then((snapshot) => {
    snapshot.docs.forEach(doc =>{
        setId=[...person,doc.data()]
      //I also tried
        setId=doc.data()
    })
})
}

return (
<>
<div className="mx-7">
<span className="font-bold text-xl"><h5>Pesquisar:</h5></span></div>
<div className="flex justify-center">
<LongText placeholder="Pesquisar Id" onChange={
    (e: ChangeEvent<HTMLInputElement>)=>setInput(e.target.value)}
    onClick={useEffect(()=>{
    setShowResults(true)
    fetchBI();
})}/>
</div>

<div className="flex justify-center">
<span className="my-4 w-11/12">
{ showResults ? <AppListId persons={person} /> : null }
</span>
</div>

</>

);

}

export default Core;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-27 15:50:56

经过漫长的日子,我找到了解决方案:我交换了这个:

代码语言:javascript
复制
 const fetchBI =  async () => {
 const ref=db.collection('id').where('numberId','==',input).get().then((snapshot) => {
snapshot.docs.forEach(doc =>{
    setId=[...person,doc.data()]

至:

代码语言:javascript
复制
const fetchBI =  async () => {
try{
var people : ID[] = []
await db.collection('id').where('numberId','==',input).get().then(
    querySnapshot=>{
        const data = querySnapshot.docs.map(
            doc=>{
            let dat = doc.data()
                people.push({
                    numberId: dat.numberId,
                    name: dat.name,
                    dateOfBirth: dat.dateOfBirth,
                    placeOfBirth: dat.placeOfBirth,
                    fathersName: dat.fathersName,
                    mothersName: dat.mothersName,
                    gender: dat.gender,
                    profession: dat.profession,
                    dateOfIssue: dat.dateOfIssue,
                    expirationDate: dat.expirationDate

                })
            })
            setId(people)
    }
   )
  }catch (error) {
 console.log(error.message)
 }
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68931762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档