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 trialJosh Reynolds
10,734 PointsImproving and Transforming NodeJS weather app with ES6 promises
Hi there, Recently completed the basics NodeJS course and did the extra credit activity of creating a weather app with Node.
I just have a few points/questions.
- How would I improve on this? First time using Node and really writing much in ES6.
- How would I implement Promises into this rather than the callbacks. I'm not to sure on how to do this.
also, my main.temp_min and temp_max don't seem to be adjusting?
Thanks
// app.js
const request = require('./weather');
let city = process.argv.slice(2);
// check if passed in one arg for state/city/zip or 2 for coords
if (process.argv.length > 2) {
request.getWeather("q=" + city);
} else {
console.error("Please pass a city, zip code or city/state as argument");
}
// weather.js
const http = require('http');
const https = require('https');
const APPID = '9c5213ce52fe7ffa15a904fc80a5f879'
function getWeather(query) {
const req = http.get('http://api.openweathermap.org/data/2.5/weather?'
+ query + '&appid=' + APPID + '&units=metric', (response) => {
let body = '';
response.on('data', (chunk) => {
body += chunk;
});
response.on('end', () => {
if (response.statusCode === 200) {
try {
const weather = JSON.parse(body);
displayWeather(weather);
} catch(err) {
console.error(err)
}
} else { // statusCode error
console.error(`Unable to get weather data. Status message: ${response.statusMessage}`);
}
});
}) // end request
}
function displayWeather(weather) {
let msg = `
Your city: ${weather.name}
Current Temp: ${weather.main.temp}
Highest: ${weather.main.temp_max}
Lowest: ${weather.main.temp_min}
`;
console.log(msg);
}
module.exports.getWeather = getWeather;
module.exports.getWithCoords = getCoordinates;
1 Answer
Thomas Nilsen
14,957 PointsI rewrote it to make use of Promises and the request module. Take a look:
const request = require('request');
//=========== CREATING THE REQUEST METHOD ===============
function getWeather(query) {
const url = `http://api.openweathermap.org/data/2.5/weather?q=${query}&appid=9c5213ce52fe7ffa15a904fc80a5f879&units=metric`;
return new Promise((resolve, reject) => {
request(url, (err, response, body) => {
let data = null;
//try to parse the data otherwise reject it with error message
try {
data = JSON.parse(body);
} catch (err) {
return reject(`Unable to parse body: ${ err } `);
}
//Resolve data If no errors and status 200 OK - Otherwise reject
if (!err && response.statusCode === 200) {
resolve(data);
} else {
reject(err || data);
}
});
});
}
//=========== LOGGING ===============
function displayWeather(weather) {
let msg = `
Your city: ${weather.name}
Current Temp: ${weather.main.temp}
Highest: ${weather.main.temp_max}
Lowest: ${weather.main.temp_min}
`;
console.log(msg);
}
//=========== MAKING THE REQUEST ===============
getWeather('Oslo')
.then(weather => {
displayWeather(weather);
})
.catch(err => {
console.log(err);
Promise.reject(err);
});