代码之家  ›  专栏  ›  技术社区  ›  FabricioG

在firebase中查询startAt和endAt的日期

  •  0
  • FabricioG  · 技术社区  · 5 年前

    我有以下数据结构:

    attendance
    --- 2020-02-09-PM
    --- 2020-02-11-PM
    --- 2020-02-16-AM
    --- 2020-02-16-PM
    --- 2020-02-18-PM
    

    以下是我的尝试:

    function getAttendanceCount(orgUid, dates) {
       orgUid.forEach(uid => {
         dates.forEach(date => {
           const getAttendCount = fDB.ref(`organization/${uid}/attendance`)
          .orderByChild('attendance')
          .startAt(date+'-AM')
          .endAt(date+'-PM')
          .once('value')
          .then(c => console.log(c.val()));
       });
     })
    }
    

    我的console.log为空。 你知道我怎么做到吗?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Peter Haddad    5 年前

    更改此:

    const getAttendCount = fDB.ref(`organization/${uid}/attendance`)
          .orderByChild('attendance')
    

    const getAttendCount = fDB.ref(`organization/${uid}/attendance`)
          .orderByKey()
    

    你需要使用的原因 orderByKey() 是因为日期是一把钥匙而不是 child 这将有一个价值示例:

    "name" : "peter"