Javascript 从firebase中的数组中删除

Javascript 从firebase中的数组中删除,javascript,firebase,react-native,google-cloud-firestore,expo,Javascript,Firebase,React Native,Google Cloud Firestore,Expo,我目前在从firebase firestore中的阵列中删除项目时遇到问题。我可以在本地删除它,但当我刷新时,它会再次显示。我知道我应该使用实际值。这是我的相关代码,这是我的firestore中的项目的外观 您遇到的问题是,arrayRemove()使用严格相等来比较数组元素并确定要删除的元素,它不像您在代码中那样比较“id”。不幸的是,这意味着每个对象都将被视为不同于其他对象(无论是不同的id还是相同的id),而不管它们有多相同({}==={}//false),因此它找不到要删除的元素arra

我目前在从firebase firestore中的阵列中删除项目时遇到问题。我可以在本地删除它,但当我刷新时,它会再次显示。我知道我应该使用实际值。这是我的相关代码,这是我的firestore中的项目的外观


您遇到的问题是,
arrayRemove()
使用严格相等来比较数组元素并确定要删除的元素,它不像您在代码中那样比较“id”。不幸的是,这意味着每个对象都将被视为不同于其他对象(无论是不同的id还是相同的id),而不管它们有多相同(
{}==={}//false
),因此它找不到要删除的元素
arrayRemove()
可以更好地处理包含基本类型(数字、字符串等)的数组

目前,最好的选择是获取现有文档,使用“id”逻辑删除所需元素并将其写回。像这样:

const removeGoalHandler=async(goalId)=>{
const existingDoc=await loansRef.doc(userId.get();
const goals=existingDoc.data().goals.filter(goal=>goal.id!==goalId);
等待loansRef.doc(userId).update({goals});
设定课程目标;
...
};

它工作过一次,但之后它一直将我想要删除的一个添加回Firebase。我现在遇到的这个问题是否与我的useEffect功能有关?您的
useEffect()
不是问题,因为它不调用任何写入数据库的函数(
set()
update()
等)。
removeGoalHandler()
函数是否是代码中唯一调用
doc().update()
的地方?不,我有另一个添加到Firebase的函数;让我现在上传,如果我注销并重新登录,贷款不会被重新添加;贷款保持删除状态。我不知道为什么会这样
const removeGoalHandler = async (goalId) => {
    let goalToDel = {}
    for(let i =0; i < courseGoals.length; i++){
        if(courseGoals[i].id == goalId){
            console.log(courseGoals[i])
            goalToDel = courseGoals[i]
        }
    }
    const removeGoal = await loansRef.doc(userId).update({
        goals: firebase.firestore.FieldValue.arrayRemove(goalToDel)
    })
    setCourseGoals((currentGoals)=> {
        return currentGoals.filter((goal)=> goal.id !== goalId)
    })
    setGoalCounter(goalCounter-1)
};

const addToFB = async (goalTitle, interestRate, years, paidOff,id) => {
    //adding data to firebase, takes into account if doc exists already 
    if(id==undefined){
        id = goalCounter
    }
    console.log('add to firebase')
    const loadDoc = await loansRef.doc(userId).get()
        .then((docSnapshot)=> {
            if(docSnapshot.exists){
                loansRef.doc(userId).onSnapshot((docu)=>{
                    console.log('num2: '+ (goalCounter+id).toString())
                    const updateLoansArr = loansRef.doc(userId).update({
                        goals: firebase.firestore.FieldValue.arrayUnion({
                            id: userId+(goalCounter+id).toString(),
                            value: goalTitle,
                            interest: interestRate,
                            years: years,
                            paidOff: paidOff
                        })
                    })
                })
            }
            else{
                console.log('num3: '+ (goalCounter+id).toString())
                const addDoc = loansRef.doc(userId).set({
                    goals: firebase.firestore.FieldValue.arrayUnion({
                    id: userId+(goalCounter+id).toString(),
                    value: goalTitle,
                    interest: interestRate,
                    years: years,
                    paidOff: paidOff
                })
            })
        }})
}
const addGoalHandler = (goalTitle, interestRate, years, paidOff,id) => {
    console.log('add goal handler')
    if(id==undefined){
        id = 0
    }
    console.log('num1: '+ (goalCounter+id).toString())
    //console.log(goalCounter)
    setGoalCounter(goalCounter+1)
    setCourseGoals((courseGoals) => [
        ...courseGoals,
        {
            id: userId + (goalCounter+id).toString(),
            value: goalTitle,
            interest: interestRate,
            years: years,
            paidOff: paidOff
        }
    ]);
    //console.log(goalCounter)
    addToFB(goalTitle, interestRate,years,paidOff,id)
    setIsAddMode(false);
}