问题描述
尝试使用arrayRemove从Firebase数组中删除项目,但出现以下错误:
ReferenceError: Can't find variable: FieldValue
const removeItemHandler = () => {
//this logs the array item like is should,dont think this value is the issue
console.log(props.item)
console.log(props.currentNote)
//removes note from the notes array
db.collection('users').doc(userEmail)
.collection('books').doc(props.currentBook)
.collection('specific').doc(props.currentNote).update({
notes: FieldValue.arrayRemove(props.item)
})
showRemoveButton(false)
}
我正在跟踪下面的链接示例,真的不确定为什么我的非常相似的情况会给我这个错误。谢谢!
https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html
此外,请记住props.item引用了数组项的名称,我用console.log的类型和值进行了检查,它是正确的。有信心这不是问题的一部分。
解决方法
您正在访问未定义的FieldValue
。您不能直接访问它。
您可以通过firebase.firestore.FieldValue
进行访问,也可以直接导入firestore.FieldValue
来访问它。因此,像这样更改代码:
const removeItemHandler = () => {
//this logs the array item like is should,dont think this value is the issue
console.log(props.item)
console.log(props.currentNote)
//removes note from the notes array
db.collection('users').doc(userEmail)
.collection('books').doc(props.currentBook)
.collection('specific').doc(props.currentNote).update({
notes: firebase.firestore.FieldValue.arrayRemove(props.item)
notes: firestore.FieldValue.arrayRemove(props.item) // or if you are imported firestore
})
showRemoveButton(false)
}