【问题标题】:TS Error: Type 'Query<DocumentData>' is not assignable to parameter of type 'DocumentReference<DocumentData>'TS 错误:类型 \'Query<DocumentData>\' 不可分配给类型 \'DocumentReference<DocumentData>\' 的参数
【发布时间】:2022-11-19 15:16:08
【问题描述】:

所以我正在尝试对 Firestore 数据运行查询。我的代码是:

import { collection, getDoc, query, where } from "firebase/firestore";
import { db } from "../../utils/firebaseConfig";

  const getQuery = async () => {
    const userDataRef = collection(db, "userData");
    const q = await query(
      userDataRef ,
      where("mailID", "==", "dashashutosh1999@gmail.com")
    );

    const users = await getDoc(q) //error

  };

  console.log(getQuery());

const users = await getDoc(q) 行中,我收到此错误:

//error description- Argument of type 'Query&lt;DocumentData&gt;' is not assignable to parameter of type 'DocumentReference&lt;DocumentData&gt;'.

我尝试搜索错误,但我只得到不同的变体,找不到有用的答案来帮助自己。

我是 TS 的新手,正在通过在我的项目中使用 TS 来学习。如果你能指导我完成这件事,我将非常感激。

【问题讨论】:

  • 请形成一个正确的问题标题,并将错误消息作为文本而不是屏幕截图放入正文。
  • 感谢您的建议:@DanielW ..我之前在文本中添加了错误。我现在强调了这一点。您还可以建议我将来应该使用哪种类型的标题?

标签: reactjs typescript firebase google-cloud-firestore


【解决方案1】:

getDoc() 函数用于使用DocumentReference 获取单个文档。要使用 Query 获取多个文档,请使用 getDocs()

const users = await getDocs(q);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2022-01-27
    • 2018-11-13
    • 1970-01-01
    • 2018-07-19
    • 2018-10-15
    • 1970-01-01
    相关资源
    最近更新 更多