javascriptnode.jsfetch-apiweather-api

Javascript and API help needed with my code: Create a module for finding current information on a particular city


import fetch from 'node-fetch'

// Constants for API details
const GEO_API_URL = 'https://wft-geo-db.p.rapidapi.com/v1/geo/cities';
const WEATHER_API_URL = 'https://weatherbit-v1-mashape.p.rapidapi.com/current';

// Separate headers for the two APIs
const GEO_HEADERS = {
  'X-RapidAPI-Host': 'wft-geo-db.p.rapidapi.com',
  'X-RapidAPI-Key': '5470f285aemshd79624e8065945cp1df6d3jsn4b3dac9e4d7c'
};

const WEATHER_HEADERS = {
  'X-RapidAPI-Host': 'weatherbit-v1-mashape.p.rapidapi.com',
  'X-RapidAPI-Key': '5470f285aemshd79624e8065945cp1df6d3jsn4b3dac9e4d7c'
};

const getCityDetails = async(cityId) => {
  try {
    let response = await fetch(${GEO_API_URL}?cityId=${cityId}, {
      method: 'GET',
      headers: GEO_HEADERS
    });

    let data = await response.json();

    if (data.data && data.data.length > 0) {
      return data.data[0];
    } else {
      throw new Error('City not found');
    }

  } catch (error) {
    console.error(Error fetching city details: $ {error.message});
    throw error;
  }
};

const getWeatherDetails = async(latitude, longitude) => {
  try {
    let response = await fetch(${WEATHER_API_URL}?lat=${latitude}&lon=${longitude}, {
      method: 'GET',
      headers: WEATHER_HEADERS
    });

    let data = await response.json();

    if (data.data && data.data.length > 0) {
      return data.data[0];
    } else {
      throw new Error('Weather data not available');
    }

  } catch (error) {
    console.error(Error fetching weather details: $ {error.message});
    throw error;
  }
};

const getCityAndWeatherInfo = async(cityId) => {
  try {
    let cityDetails = await getCityDetails(cityId);
    let weatherDetails = await getWeatherDetails(cityDetails.latitude, cityDetails.longitude);

    return {
      population: cityDetails.population,
      elevation: cityDetails.elevationMeters,
      currentTemperature: weatherDetails.temp
    };
  } catch (error) {
    console.error(Error fetching combined data: $ {error.message});
  }
};

// Example usage
getCityAndWeatherInfo('Q5465')
  .then(data => {
    console.log(data);    
  });

The question is as follows:

Create a module for finding current information on a particular city. Information for the city, including coordinates, can be found using this API (https://rapidapi.com/wirefreethought/api/geodb-cities/).

I subscribed to the above as I need to do.

Something in the code is not working and currently I do not know where to search for the problem anymore.


Solution

  • As much as I understand, 'Something in the code is not working' you mean that the code is not getting compiled properly, right?

    There are compilation errors because of the console.error messages. You need to use backticks `` to use ${} in your console error messages.

    Try changing this:

    console.error(Error fetching weather details: ${error.message});

    to this:

    console.log(`Error fetching city details: ${error.message}`);

    You can learn more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals