Skip to content

Commit db3f4b3

Browse files
API - order data by creation date + doc links
1 parent 346a9e5 commit db3f4b3

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

weather-api/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ const weatherDb = db.collection("weather-data");
2525

2626
app.get("/weather/limit/:limit", async (req, res) => {
2727
const limit = parseInt(req.params.limit);
28-
const query = weatherDb.limit(limit);
28+
const query = weatherDb.orderBy("created", "desc").limit(limit);
2929
const querySnapshot = await query.get();
30-
// if (querySnapshot.exists) { // why it doesn't work?
30+
// if (!querySnapshot.empty) { // .empty property works with Firestore
31+
// Dock for QuestySnapshot object: https://googleapis.dev/nodejs/firestore/2.2.3/QuerySnapshot.html
32+
// Dock for querySnapshot.docs: https://googleapis.dev/nodejs/firestore/2.2.3/QueryDocumentSnapshot.html
33+
// if (querySnapshot.exists) { // .exists is doc property, doesn't work for querySnapshot
3134
if (querySnapshot.size > 0) {
3235
// .docs are not simple objects, they contains also prototypes, metadata & timestamps
3336
// We iterate every doc.data() to retrieve all fields in the document as an simple Object.
@@ -40,7 +43,9 @@ app.get("/weather/limit/:limit", async (req, res) => {
4043

4144
app.get("/weather/place/:placeId", async (req, res) => {
4245
const placeId = req.params.placeId;
43-
const query = weatherDb.where("placeId", "==", placeId);
46+
const query = weatherDb
47+
.where("placeId", "==", placeId)
48+
.orderBy("created", "desc");
4449
const querySnapshot = await query.get();
4550
if (querySnapshot.size > 0) {
4651
const weatherData = querySnapshot.docs.map((doc) => doc.data());

0 commit comments

Comments
 (0)