【问题标题】:Firebase v9 React JS IndexOf is not a functionFirebase v9 React JS IndexOf 不是函数
【发布时间】:2021-11-03 05:06:04
【问题描述】:

由于某种原因,它在控制台中向我抛出了一个错误“TypeError:n.indexOf 不是函数”,我不太明白为什么会发生这种情况。我正在使用 Firestore 和 Firebase v9 Modular,基本上它是一个在 Firestore 数据库中创建一个类别的函数,并且在该类别中创建一个测试任务。

这是完整的错误:

TypeError: n.indexOf is not a function
    at Function.fromString (index.esm2017.js:1032)
    at va (index.esm2017.js:14990)
    at App.jsx:48

而错误信息中的第48行是:

const taskRef = doc(collection(db, "categorias"), where("uid", "==", currentUser), newDocRef.id)

这是我的代码:)

const currentUser = '1234'

const crearCategoria = useCallback(async() => {
    try {
      const newDocRef = doc(collection(db, "categorias"));

      await setDoc(newDocRef, {
        name: categoryName,
        uid: currentUser,
        projectId: newDocRef.id
      })

      const taskRef = doc(collection(db, "categorias"), where("uid", "==", currentUser), newDocRef.id)
      await setDoc(taskRef, {
       name: 'prueba',
       uid: currentUser,
       projectId: newDocRef.id
    })

    } catch (error) {
      console.log(error)
    }
  })

【问题讨论】:

  • 你想做什么?看来您正在更新不可能的文档 where("uid", "==", currentUser) 。您首先需要参考这些文件。
  • 我在使用 Firestore v9 时遇到了同样的“TypeError: n.indexOf is not a function”错误,我在尝试解决这个问题时遇到了这个问题。为将来发现此问题的其他人分享反馈:我喜欢 Firestore 和 Firebase,但有时很难弄清楚错误消息告诉您什么。在这种情况下,“TypeError: n.indexOf is not a function”表示您的文档引用无效。解决此特定示例的正确方法是在下面 Frank 的回答中,但更一般地说,如果您看到此错误,则需要找出您的 doc ref 错误的原因。

标签: reactjs firebase google-cloud-firestore


【解决方案1】:

正如 Dharmaraj 所说,您无法对查询执行更新。相反,您必须:

  1. 执行查询以查找匹配的文档。
  2. 循环访问应用程序代码中的文档。
  3. 依次更新它们。

在代码中应该是这样的:

const taskQuery = doc(collection(db, "categorias"), where("uid", "==", currentUser))
const taskDocs = await getDocs(taskQuery)
taskDocs.forEach((taskDoc) => {
  await setDoc(taskDoc.ref, {
    name: 'prueba',
    uid: currentUser,
    projectId: newDocRef.id
  })
})

【讨论】:

    猜你喜欢
    • 2022-08-17
    • 2022-12-15
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2021-04-10
    • 2021-11-13
    相关资源
    最近更新 更多