Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Google API for extra credit problem

// get google map api 
const zipRequest = https.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + `${address}` + '&key=' + `${zipKey}`, response => {
  let lat = '';
  let lng = '';
  let zipBody = '';

  response.on('data', data => {
    zipBody += data.toString();
  });

  response.on('end', () => {
    const zipData = JSON.parse(zipBody);

    console.dir(zipData);
  });
});

I was able to parse the data but after I am having problem accessing data such as lat and lng.

Steven Parker
Steven Parker
231,007 Points

This code is not complete. Do you have the whole project in a workspace or repo where it could be tried out?

1 Answer

Steven Parker
Steven Parker
231,007 Points

You never assign lat or lng except with empty strings.

is there more to this code that you didn't show?

coordinate.lat = zipData.results[0].geometry.location.lat;
coordinate.lng = zipData.results[0].geometry.location.lng;

this worked!! Thank you.