【问题标题】:Firestore query having problem with getting the data I need on react nativeFirestore 查询在获取我需要的 react native 数据时遇到问题
【发布时间】:2021-05-29 13:50:48
【问题描述】:
await firebase
      .firestore()
      .collection("patients")
      .doc(firebase.auth().currentUser.uid)
      .collection("appointment")
      .orderBy("dateTime", "asc")
      .get()
      .then((result) => {
        let appointment = result.docs.map((doc) => {
          const data = doc.data();
          const id = doc.id;
          const currentDate = moment().format("YYYY-MM-DD");
          const dt=new moment(doc.data().dateTime,'YYYY-MM-DD').format('YYYY-MM-DD');
          if(dt===currentDate){
            return { id, ...data };
          }
        });
        dispatch({ type: GET_APPOINTMENT, appointment });
      });

我想同时做三件事:

  • 在一个对象中获取今天的所有约会
  • 在对象中获取今天之后的所有约会
  • 当约会超过当前时间时,我想从数据库中删除它

怎么做?

【问题讨论】:

  • 您的数据库中的dateTime 是什么类型?它是 Firestore 时间戳、Javascript 日期还是自 01/01/1970 以来的毫秒数?
  • 我将其作为字符串发送,例如 '2021-02-27-04:30',然后在返回时将其更改为时刻对象

标签: reactjs firebase react-native google-cloud-firestore react-redux


【解决方案1】:

您可以做的是在单个查询中获取用户集合中的所有文档,然后遍历每个文档并根据它是过去、今天还是将来对其进行排序。

这是一个例子,由 cmets 解释发生了什么。

const db = firebase.firestore();

db
  .collection("patients")
  .doc(firebase.auth().currentUser.uid)
  .collection("appointment")
  .orderBy("dateTime", "asc")
  .get()
  .then(async (result) => {
    const pastAppointmentRefs = [], todayAppointments = [], futureAppointments = [];

    // Get current date in YYYY-MM-DD and milliseconds, just the once
    const currentDate = moment().format("YYYY-MM-DD");
    const currentDateMs = Date.now();
    
    result.forEach((doc) => {
      // Get appointment datetime and it's equivalent in milliseconds
      const dateTime = doc.get("dateTime"); // format: YYYY-MM-DD-HH:mm
      const dateTimeMs = moment(dateTime, 'YYYY-MM-DD-HH:mm').valueOf();
    
      // sort appointment based on dateTime
      if (dateTimeMs < currentDateMs) {
        // is in the past, could also be currently taking place
        pastAppointmentRefs.push(doc.ref);
      } else if (currentDate === dateTime.slice(0,10)) {
        // is today
        todayAppointments.push({ id: doc.id, ...doc.data() });
      } else {
        // is in the future
        futureAppointments.push({ id: doc.id, ...doc.data() });
      }}
    });

    // if there are any found appointments in the past, delete them all
    if (pastAppointmentRefs.length > 0) {
      const batch = db.batch();
      // warning! you can only delete up to 500 docs in a single batch
      pastAppointmentRefs.forEach((ref) => {
        batch.delete(ref);
      });
      await batch.commit(); // commit the changes (the deletions)
    }

    // fire the event
    dispatch({
      type: GET_APPOINTMENTS,
      today: todayAppointments,
      future: futureAppointments
    });
  });

【讨论】:

  • 删除功能没有起作用,不幸的是错误是[未处理的承诺拒绝:ReferenceError:找不到变量:db]
  • @OsamaTab 在编写此答案时,代码按预期运行。如果您没有像我一样在此答案的顶部声明const db = firebase.firestore(),则需要将const batch = db.batch() 更改为const batch = firebase.firestore().batch()
  • 抱歉没有看到 db 被声明我现在修复它谢谢你的帮助
猜你喜欢
  • 2021-06-23
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2020-08-05
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多