Muhammad Naufil
Muhammad Naufil

Reputation: 2647

Nothing to geocode - Weather API - Nodejs

I am new to Nodejs and trying to use weather api.

When I test the link in browser, it gives me accurate answer

http://api.openweathermap.org/data/2.5/weather?q=karachi&appid=dcf486a78f2b8e898c4b1a464a1b31e1

while it keeps throwing error.

const express = require("express")
var logger = require("morgan")
var path = require("path")
var bodyParser = require("body-parser")
let requested = require('request');

var app=express()
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
  }));

app.set("views", path.resolve(__dirname,"views"))
app.set("view engine",'ejs')

app.use(logger("short"))

app.get("/",function(request,response)
{
    response.render("homepage")
})

app.post('/', function(request, response) {
    var urlOpenWeatherCurrent = 'http://api.openweathermap.org/data/2.5/weather?'
    var queryObject = {
        APPID: "dcf486a78f2b8e898c4b1a464a1b31e1",
        city: request.body.cityName
    }
    console.log(queryObject)
    requested({
        url:urlOpenWeatherCurrent,
        q: queryObject           // In many tutorials they used 'qs' instead of 'q'. I don't know why.
    }, function (err, response, body) {
    // response.send('You sent the name ' + request.body.cityName + ".");
    if(err){
      console.log('error:', error);
    } else {
      console.log('body:', JSON.parse(body));

    }
  });
});


app.use(function(request,response)
{
    response.status(404)
    response.send("Error")
})


app.listen(3000,()=>console.log("Working"))

Error

{ APPID: 'dcf486a78f2b8e898c4b1a464a1b31e1', city: 'karachi' }
'Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.'

If I change q to qs in nodejs, then

{ APPID: 'dcf486a78f2b8e898c4b1a464a1b31e1', city: 'karachi' }
body: { cod: '400', message: 'Nothing to geocode' }

Note that changing q to qs in raw html API link also gives

{"cod":"400","message":"Nothing to geocode"}

I believe from the response that I should use qs in nodejs, because at least this time it is not considering API key wrong. But in the API link, we have q and not qs. So how come qs makes sense? Besides, as far as I Understood, it is not properly concatenating the API strings together, resulting in the malfunctioning. But I don't know how to print the entire link in console to validate what I just said.

views/homepage.ejs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form class="pure-form" action="/" method="POST">
        <input type="name" placeholder="City name" name="cityName" autofocus required>
        <input type="submit" valuue="Go">
    </form>
</body>
</html>

Upvotes: 2

Views: 3014

Answers (1)

embed the "q" and "api key" with open weather url like "http://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&appid=${apiKey}" also Check this link https://codeburst.io/build-a-weather-website-in-30-minutes-with-node-js-express-openweather-a317f904897b

Upvotes: 3

Related Questions