FirebaseError:预期类型为"Ta",但它是:自定义 oh 对象



我正在从名为matches的firebase集合中检索数据,我正在根据用户查询数据。但是我得到一个错误FirebaseError:期望类型'Ta',但它是:一个自定义对象

import { View, Text } from "react-native";
import React, { useEffect, useState } from "react";
import useAuth from "../hooks/useAuth";
import { onSnapshot, query, where } from "firebase/firestore";
import { db } from "../firebase";
const Chatlist = () => {
const [matches, setMatches] = useState([]);
const { user } = useAuth();
useEffect(
() =>
onSnapshot(
query(db, "matches"),
where("userMatched", "array-contains", user.uid)
),
(snapshot) =>

setMatches(
snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}))

),
[user]

);

查询约束应该在query()函数本身中传递。onSnapshot的第二个参数是回调函数。尝试重构代码,如下所示:

useEffect(() => {
if (user) {
const q = query(collection(db, "matches"), where("userMatched", "array-contains", user.uid))
return onSnapshot(q, (snapshot) => {
setMatches(
snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}))
)
})
} else {
console.log("No user logged in")
}
}, [user])

最新更新