我目前无法从我的 firebase Firestore 中的数组中删除一个项目。我可以在本地删除它,但是当我刷新时它又出现了。我知道我应该使用实际值。这是我的相关代码,这是我的 Firestore 中的项目的样子
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
})
})
})
}