How to query firebase realtime database to get last month data

Issue

This Content is from Stack Overflow. Question asked by mazenqp

How to query data last month or last 2 months based on the timestamp in snapshot this is the question, know I have been able to do that in Firestore like this

useEffect(() =>{
const fetchData = async () => {

const today = new Date()
const lastMonth = new Date(new Date().setMonth(today.getMonth() - 1))
const prevMonth = new Date(new Date().setMonth(today.getMonth() - 2))

const lastMonthQuery = query(collection(db,'users'), where('since', '<=', today), where('since', '>', lastMonth))

const lastMonthData = await getDocs(lastMonthQuery)

}
fetchData()
},[])

how can i wright query like this in realtime database i have tried this code and this is how my database look like

enter image description here

useEffect(()=>{
    const fetchData = async () =>{
      const today = new Date()
      const lastMonth = new Date(new Date().setMonth(today.getMonth() - 1))
      const prevMonth = new Date(new Date().setMonth(today.getMonth() - 2))

      try{
        const dbRef = query(ref(database, 'users/since'), startAt(lastMonth), endAt(today));
        const last = await get(dbRef)
        console.log(last)
}catch(err){
        console.log(err)
      }
    }
    fetchData()
  },[])

in the console i got this

Error: Query: When ordering by priority, the first argument passed to startAt(), startAfter() endAt(), endBefore(), or equalTo() must be a valid priority value (null, a number, or a string).



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?