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

How to pass data between modules in node.js

OK! Something is escaping me.

My capstone project is designed to add value to the BnB we run out of our home. We want to put a tool in the hands of our guests that will give them a means of looking at the weather in our area, both now and for the next week so they can plan what to wear when they go adventuring around the area. We want them to be able to look up restaurants in the local area and find reviews that will help them decide where to go for meals (we are a BnB we don't feed them). And, finally, we want them to be able to look up all the local 'places to go' and 'things to see'.

All of this functionality is based on geolocation, requiring our address as a base and our location coordinates.

I am trying to build a module that will return three things:

geocode.loc (which is the human readable geocode location)
geocode.lat (which is the latitude associated with the location)
geocode.lng (which is the longitude associated with the location)

These data points will be passed throughout my app to the other apis I am using:

a 'weather' api to return local weather
a 'restaurants' api to return local restaurants
an 'attractions' api to return local attractions

Here is the code in question:

'use strict';
//  this module connects to the Google geocode api and returns the formatted address and latitude/longitude for an address passed to it

const request = require('request'),
    req_prom  = require('request-promise');

const config  = require('../data/config.json');

const geocode_loc = 'Seattle, WA';
const geocode_key = config.GEOCODE_KEY;

const options = {
    url: `https://maps.google.com/maps/api/geocode/json?address=${geocode_loc}&key=${geocode_key}`,
    json: true
};

let body = {};

let geocode = request(options, (err, res, body) => {
    if (!err && res.statusCode === 200) {
        body = {
            loc: body.results[0].formatted_address,
            lat: body.results[0].geometry.location.lat,
            lng: body.results[0].geometry.location.lng
        };
        return body;
    }
});

console.log(geocode);

module.exports.geocode = geocode;