找不到变量FieldValue,React Native / Firebase

问题描述

尝试使用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)

    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...