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 trialPatrik Horváth
11,110 PointsNode.JS API and CITY code is wrong and api key is wrong
hi, i have problem when i type variable in ${ } i getting error because its wrong api.key and its NOT ! when i type it directly to get method it works perfect
const https = require("https");
const api = require("./api.json");
function get(query) {
const request = https.get('https://api.openweathermap.org/data/2.5/weather?zip=${query}&appid=${api.key}', (response) => {
let body = "";
response.on("data", chunk => {
body += chunk;
});
response.on("end", () => {
console.log(body);
});
});
}
api.json :
{
"key" : "385a38ead92aa38a3e4e857ec3323062"
}
2 Answers
Neil McPartlin
14,662 PointsFor template literals to work, in the request declaration, you need to replace those single quotes with back ticks.
//Current
const request = https.get('https://api.openweathermap.org/data/2.5/weather?zip=${query}&appid=${api.key}',
//New
const request = https.get(`https://api.openweathermap.org/data/2.5/weather?zip=${query}&appid=${api.key}`,
Sean T. Unwin
28,690 PointsIs api.json
in the same directory as the js file you are calling it from? I ask because the ./
in const api = require("./api.json");
means to use the current directory. Please check that this is not a path issue, meaning the file could not be found.
You could try a console.log(api.key);
after declaring the api
variable to ensure the api key is accessed correctly.
Patrik Horváth
11,110 PointsYup it is in same directory also and console log works and i tryed ive API manualy to link and just put ${query} and it telling me this country no exist but if i put it directly to link it works and my Input is Same as Manual input to link =\
Sean T. Unwin
28,690 PointsWhat is the value of query
if you console.log
it? Perhaps this is where the issue lies.
Patrik Horváth
11,110 PointsInteresting log is writed before CODE :
const weather = require("./weather");
const query = process.argv.slice(2).join("_").replace(" ", "_");
weather.get(query);
console.log("LOG:" + query);
C:\Users\Snezn\IdeaProjects\Node.JS\1 - Weather app>node app.js 94040
LOG: 94040
{"cod":"404","message":"city not found"}
Patrik Horváth
11,110 PointsPatrik Horváth
11,110 Pointsyour "`" is not valid in programing world
you ahve to use ALT + 39 '
Neil McPartlin
14,662 PointsNeil McPartlin
14,662 PointsPatrik,
Just watch the first 35 seconds of this video... [https://teamtreehouse.com/library/basic-and-multiple-line-strings]
and/or read this... [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals]
Patrik Horváth
11,110 PointsPatrik Horváth
11,110 Pointsinteresting maybe i missed it in ES6 course, Thanks