firebase 使用arrayRemove()从firestore 9数组中删除对象?

vs3odd8k  于 2023-02-09  发布在  其他
关注(0)|答案(2)|浏览(87)

我尝试从firestore中的数组中删除一个对象,但遇到了一个障碍,执行删除操作的要求或引用是什么?对象中的一个键值是否足以执行删除操作,或者该对象是否应与要删除的对象相同?

const deleteWeek = async () => {
        const docRef = doc(db, 'Weeks', id);
        await updateDoc(docRef, {
          weeks: arrayRemove({
            weekId: '7518005f-7b10-44b6-8e0a-5e41081ee064',
          }),
        });
      };
      deleteWeek();
    }

但是,数据库中周如下所示

{name ,"Week 2"
days : [/*data all kinds*/]
weekId : "7518005f-7b10-44b6-8e0a-5e41081ee064"}
jaxagkaj

jaxagkaj1#

如果它是一个对象数组,那么你需要知道整个对象来使用arrayRemove()。例如,如果一个文档看起来像这样:

{
  ...data
  weeks: [
    { 
      name: "Week 2",
      days: [/*data all kinds*/]
      weekId: "7518005f-7b10-44b6-8e0a-5e41081ee064"}
    }
  ]
}

您必须在arrayRemove()中传递整个week对象,将这些数据存储在子集合中可能更好,这样您就可以查询/删除特定的数据。

jhdbpxl9

jhdbpxl92#

由于firestore中没有只删除array中一个元素的函数,因此需要让arrayRemove引用要删除的同一个对象,然后创建一个新对象,并使用arrayUnion方法插入
就我而言,我习惯于

const { leave,date,email } = req.body;

  const attendanceRef = admin.firestore().collection('Attendance').doc(`${email}`);
  
  const attendanceData = await attendanceRef.get();

  const attendanceRecord = attendanceData.data().attendance;

  const removeTarget = attendanceRecord.find((item) => item.date === date);

  await attendanceRef.update({
    attendance: admin.firestore.FieldValue.arrayRemove(removeTarget),
  })

  const obj = {
    ...removeTarget,
    "leave": leave,
  }

  await attendanceRef.set({
    attendance: admin.firestore.FieldValue.arrayUnion(obj),
  },{ merge: true })
  const newAttendance = await attendanceRef.get();
  const newAttendanceRecord = newAttendance.data().attendance;

  return await res.json({
    message: '퇴근시간이 저장되었습니다.',
    attendance:newAttendanceRecord
  });

更新后,它可能如果错误发生.如果错误发生,你需要所有的工作取消.这种情况下,你可能想使用批处理方法

const admin = require('firebase-admin');

module.exports = async function(req,res) {
  const { leave,date,email } = req.body;

  const batch = admin.firestore().batch();

  const attendanceRef = admin.firestore().collection('Attendance').doc(`${email}`);
  
  const attendanceData = await attendanceRef.get();

  const attendanceRecord = attendanceData.data().attendance;

  const removeTarget = attendanceRecord.find((item) => item.date === date);

  // await attendanceRef.update({
  //   attendance: admin.firestore.FieldValue.arrayRemove(removeTarget),
  // })
  batch.update(
    attendanceRef,{ attendance: admin.firestore.FieldValue.arrayRemove(removeTarget) }
  )

  const obj = {
    ...removeTarget,
    "leave": leave,
  }
  
  // await attendanceRef.set({
  //   attendance: admin.firestore.FieldValue.arrayUnion(obj),
  // },{ merge: true })
  batch.set(
    attendanceRef, { attendance: admin.firestore.FieldValue.arrayUnion(obj) },{ merge: true }
  )

  await batch.commit();

  const newAttendance = await attendanceRef.get();
  const newAttendanceRecord = newAttendance.data().attendance;
  return await res.json({message: '퇴근시간이 저장되었습니다.',attendance:newAttendanceRecord});
}

希望能帮到你

相关问题