0

I'm using geonames API and get data from it with a post request. Now I want to take part of these data (lat, long) and pass it through another API (weatherbit API) as my project need to pass (lat and long) in URL and get data from it I try to make a get request in server-side to grab information and secure api key but it give me undefined. the code gives me undefined in URL parameters

(EX. https://api.weatherbit.io/v2.0/forecast/daily/&lat=undefined&lon=undefined&key=API_KEY)

here my code How can I do this?

Edit: If I press the button again the request worked fine. So I have to press button 2 times before I get the lat and long data from first API. How to solve this>

first hit on button results

second hit on button results

in client-side

const getWeatherIoURL = async (url) => {
  const response = await fetch(url);
  try {
    const data = await response.text();
    //console.log(data);
    return data;
  } catch (error) {
    console.error(error);
  }
};
export default getWeatherIoURL;

my code in server-side for both post request and get request

weatherData = {}
app.post('/geoNamesData', (request, response) => {
  weatherData['date'] = new Date();
  weatherData['countryName'] = request.body.countryName;
  weatherData['latitude'] = request.body.latitude;
  weatherData['longitude'] = request.body.longitude;
  response.send(weatherData);
});

//console.log(`your api is: ${process.env.API_WEATHER_KEY}`);

app.get('/weatherURL', (request, respond) => {
  const url_forecast = 'https://api.weatherbit.io/v2.0/forecast/daily';
  const lat = `lat=${weatherData.latitude}`;
  const long = `lon=${weatherData.longitude}`;
  const url = `${url_forecast}?${lat}&${long}&key=${process.env.API_WEATHER_KEY}`;
  console.log(url);

  respond.send(url);
});

my code structure

import geoNamesData from './geoNames_data';
import getWeatherIoURL from './weather_bit';
import postData from './post_geoNames';
//import exposeWeatherIoForecast from './weather_bit'

export function main() {
  document.getElementById('search').addEventListener('click', (event) => {
    event.preventDefault();
    const baseURL = 'http://api.geonames.org/searchJSON?q=';
    const API_Credentials = '&username=*********';
    const destination = document.querySelector('#destination').value;


    geoNamesData(baseURL, destination, API_Credentials).then((data) => {
        //console.log(data);
        postData('/geoNamesData', {
          date: new Date(),
          countryName: data.countryName,
          latitude: data.lat,
          longitude: data.lng,
        });
      });
      getWeatherIoURL('/weatherURL').then((data) => {
        console.log(data);
        exposeWeatherIoForecast(data);
      });
 }


the flow of my code is get latitude, longitude and country name for specific city entered by user using geonames API then use the latitude and longitude from this request as a parameters in another API (wathearbit) - which need these data to complete the request - to get the current or forecast weather for the city and update UI with these information ** city, Country Name , weather **

  • But you're not doing anything with the post request. You're just taking in the request body and send the same thing back to the client. It doesn't really make sense to me. Could you please clarify the flow a little bit more? – Algo7 Apr 20 '20 at 09:17
  • @Aviv Lo the flow of my code is get latitude, longitude and country name for specific city entered by user using geonames API then use the latitude and longitude from this request as a parameters in another API (wathearbit) - which need these data to complete the request - to get the current or forecast weather for the city and update UI with these information ** city, Country Name , weather ** – Mohammed Khairy Apr 20 '20 at 09:48

1 Answers1

0

Perhaps put your function expressions at the top before calling them. Function declarations get hoisted while expressions don't.

const geoNamesData = async (url, destination, api) => {
  const response = await fetch(url + destination + api);
  try {
    const data = await response.json();
    console.log(data.geonames[0]);
    return data.geonames[0];
  } catch (error) {
    console.error(error);
  }
};

const getWeatherIoURL = async (url) => {
  const response = await fetch(url);
  try {
    const data = await response.text();
    //console.log(data);
    return data;
  } catch (error) {
    console.error(error);
  }
};

geoNamesData(baseURL, destination, API_Credentials)
        .then((data) => {
          //console.log(data);
          postData('/geoNamesData', {
            date: new Date(),
            countryName: data.countryName,
            latitude: data.lat,
            longitude: data.lng,
          });
        })

      getWeatherIoURL('/weatherURL')
        .then(data=>{
          console.log(data)
          exposeWeatherIoForecast(data);
        })
Algo7
  • 2,122
  • 1
  • 8
  • 19
  • I checked the format if I replace the the code ``` `lat=${weatherData.latitude}` ``` with a hard coded data just ``` '50.123' ``` the code work perfect and get the data I need so if I console log the URL i get (ex: ***********/&lat=1234&lon=12345&key=API_KEY) otherwise if I put ``` `lat=${weatherData.latitude}` ``` I get URL (ex: ***********/&lat=1234&lon=12345&key=API_KEY) – Mohammed Khairy Apr 20 '20 at 09:57
  • if I remove the API key https://api.weatherbit.io/v2.0/forecast/daily?lat=undefined&lon=undefined – Mohammed Khairy Apr 20 '20 at 10:07
  • Exactly. It means that you're not getting the correct lat & long from the user's input. That's why it's saying undefined. – Algo7 Apr 20 '20 at 10:11
  • the user don't enter the lat and long. the user only enter city name and the first API get the lat and long and it suppose to pass these as parameters in the second API – Mohammed Khairy Apr 20 '20 at 10:29
  • I can't see difference from my code I will edit the post for main.js file please take a look at it – Mohammed Khairy Apr 20 '20 at 10:58
  • The difference is that you call `geoNamesData` after both `geoNamesData` and `geoNamesData ` are declared. – Algo7 Apr 20 '20 at 11:04
  • Send us a screenshot of the network tabs in the dev console. – Algo7 Apr 20 '20 at 11:10
  • Did you check the network tabs in the dev console? Check the http request cotent. – Algo7 Apr 20 '20 at 11:15
  • Then click on the one that has a status code of 400. What does it say? – Algo7 Apr 20 '20 at 11:20
  • {error: "Invalid lat/lon supplied."} error: "Invalid lat/lon supplied." – Mohammed Khairy Apr 20 '20 at 11:56
  • I see something in network tab. geoNamesData comes after weatherURL. I think this is the problem. – Mohammed Khairy Apr 20 '20 at 12:01