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!