Yehuda Yefet
Yehuda Yefet

Reputation: 31

nodejs async await not returning array

I am able to console.log the output properly, but when I try to return the array, it fails (ie: no output)


    //get all geo route
    app.get('/api/geo', function(req, res){
       res.send(getGeo());
    });

    async function getGeo() {
      let query = firestore.collection('geo'); //.where('foo', '==', 'bar');

      let response = await query.listDocuments().then(documentRefs => {
        return firestore.getAll(...documentRefs);
      }).then(documentSnapshots => {
         let geomatches = [];
         for (let documentSnapshot of documentSnapshots) {
            if (documentSnapshot.exists) {
              //console.log(`Found document with data: ${documentSnapshot.id}`);
              geomatches[documentSnapshot.id] = documentSnapshot.data();
            }//if
         }//for
         return geomatches;
      });

      return response;

    }

Upvotes: 0

Views: 115

Answers (2)

Rashomon
Rashomon

Reputation: 6762

getGeo() is an async function. You should use await to call it. Also declare your route callback as async:

//get all geo route
app.get('/api/geo', async function(req, res){
   res.send(await getGeo());
});

Upvotes: 1

Sai Jeevan Balla
Sai Jeevan Balla

Reputation: 145

use await

  //get all geo route
   app.get('/api/geo', async function(req, res){
      res.send( await getGeo());
   });

Upvotes: 1

Related Questions