我正在做一个React项目,在输入一个值然后单击search按钮后,应用程序搜索数据库中是否存在id。如果是,则在同一页中显示搜索结果。我很难分配搜索的值,然后显示它。当我试图将搜索结果分配给一个数组时,它会给出一个错误:
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变量中。
以下是代码:
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;发布于 2021-08-27 15:50:56
经过漫长的日子,我找到了解决方案:我交换了这个:
const fetchBI = async () => {
const ref=db.collection('id').where('numberId','==',input).get().then((snapshot) => {
snapshot.docs.forEach(doc =>{
setId=[...person,doc.data()]至:
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)
}
}https://stackoverflow.com/questions/68931762
复制相似问题