0

I need to retrieve from firebase some objects contained in an array by selecting only those within a certain time range. I checked the firebase documentation but when I try to use my code to send a GET request, it returns nothing. I need some help to understand what I am doing wrong and what's the right way to make this kind of query with firebase and node.js.

Here you can see how my data are structured and my code for the GET request.

enter image description here

export const getLastWeekDataFromDatabase = async (req: IdRequest, res: Response) => {
    
    const deviceId = req.params.deviceId
    
    try {
        const reference = db.collection("AQData")
        .doc(deviceId)
        .collection("history")
        
        const document = await reference
        .orderBy("timestamp").startAt(1660483660946).endAt(1660483712221)
        .get()
        
        res.status(200).json(document.forEach(doc => {
            doc.data()
        }))
        
    } catch(error) {
        res.status(500).json(error)
    };
};
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Lia189
  • 41
  • 9
  • It's not possible to use map fields inside arrays in a Firestore query filter or ordering. You will have to use a different structure in order to satisfy such a query. Consider, instead of using an array, use a subcollection with each map being an individual document. – Doug Stevenson Aug 19 '22 at 15:26
  • Thnaks for the reply. I reworked the structure of my documents as you suggested. Each document contains now a subcollection called "history" that stores the data history for that specific document. Still, when i perform a query, i get a 200 response although without body. – Lia189 Aug 19 '22 at 17:53
  • You should ask a new question with your current situation and code that doesn't work the way you expect. This question is closed as a duplicate. – Doug Stevenson Aug 19 '22 at 18:58

0 Answers0