Monday, 13 March 2017

For loop returning null in node js

I am using googles api to get 3 fields, name rating and place review. Since place review uses another api call I created it as a function to get the data inside my place_review.

response.results.forEach((entry)=>{
         var restaurantName = {
          "name" : entry.name,
          "rating" : entry.rating,
          "place_review" : placeReview(entry.place_id)
        }
        arr.push(restaurantName);
        console.log(restaurantName);// shows only name and rating
      });

The extra function

function placeReview(place_id){
   console.log(place_id) // i see all place id via forloop.
   googlePlaces.placeDetailsRequest({placeid: place_id},function(error,response){
    if (error) throw error;
     return response.result.reviews[0].text;
  });
}

When I run the code , i get name : somename, rating : 4.5 and place_review : null

I thought the forloop will call the function and the return of the function will append the string to "place_review" not sure what I am doing wrong.



via Masnad Nehith

No comments:

Post a Comment