@@ -25,9 +25,12 @@ const weatherDb = db.collection("weather-data");
25
25
26
26
app . get ( "/weather/limit/:limit" , async ( req , res ) => {
27
27
const limit = parseInt ( req . params . limit ) ;
28
- const query = weatherDb . limit ( limit ) ;
28
+ const query = weatherDb . orderBy ( "created" , "desc" ) . limit ( limit ) ;
29
29
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
31
34
if ( querySnapshot . size > 0 ) {
32
35
// .docs are not simple objects, they contains also prototypes, metadata & timestamps
33
36
// 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) => {
40
43
41
44
app . get ( "/weather/place/:placeId" , async ( req , res ) => {
42
45
const placeId = req . params . placeId ;
43
- const query = weatherDb . where ( "placeId" , "==" , placeId ) ;
46
+ const query = weatherDb
47
+ . where ( "placeId" , "==" , placeId )
48
+ . orderBy ( "created" , "desc" ) ;
44
49
const querySnapshot = await query . get ( ) ;
45
50
if ( querySnapshot . size > 0 ) {
46
51
const weatherData = querySnapshot . docs . map ( ( doc ) => doc . data ( ) ) ;
0 commit comments