【发布时间】: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