0

I'm trying to get data from firestore. Because it I am trying to retrieve based on a date range (fromDate and toDate), I decided to use two separate queries. The first one gets weddings before the ToDate, and the second gets weddings after the fromDate. I then want to add results from both in a SET, to avoid duplicates. But when I use response.json and pass in the resultSet, it comes up empty. If I change resultsSet to a list though, it spits out the data I am looking for.

Here is the code I have right now:

    exports.getWeddingsBasedOnDateRange = (request, response) => {
  const weddingSearchParams = {
    fromDate: new Date(request.body.fromDate),
    toDate: new Date(request.body.toDate),
    guests: request.body.guests
  };

  let weddingsBeforeToDate = [];
  let weddingsAfterFromDate = [];

  db.collection("weddings")
    .where("toDate", "<=", weddingSearchParams.toDate)
    .where("guests", "==", weddingSearchParams.guests)
    .get()
    .then(data => {
      data.forEach(doc => {
        weddingsBeforeToDate.push({
          ...doc.data(),
          weddingId: doc.id
        });
      });
      db.collection("weddings")
        .where("fromDate", ">=", weddingSearchParams.fromDate)
        .where("guests", "==", weddingSearchParams.guests)
        .get()
        .then(data => {
          data.forEach(doc => {
            weddingsAfterFromDate.push({
              ...doc.data(),
              weddingId: doc.id
            });
          });
          let resultSet = new Set();
          resultSet.add(weddingsBeforeToDate);
          resultSet.add(weddingsAfterFromDate);
          return response.json(resultSet);
        })
        .catch(err => console.error(err));
    })
    .catch(err => console.error(err));
};

I couldn't find anything online about set compatibility with response.json but I don't see why it can't take a Set.

Any help would be greatly appreciated!

Sishir Mohan
  • 59
  • 1
  • 4

1 Answers1

1

You can not convert a JavaScript Set to a JSON format because the data stored in the set is not stored as properties. You can code any one the following:

JSON.stringify([...s]);
JSON.stringify([...s.keys()]);
JSON.stringify([...s.values()]);
JSON.stringify(Array.from(s));
JSON.stringify(Array.from(s.keys()));
JSON.stringify(Array.from(s.values()));

This is a direct copy of:

JSON stringify a Set

Kolban
  • 13,794
  • 3
  • 38
  • 60