0

I'm trying to build a weather app in nodejs with dark-sky API. I got a separate js file and keep my forecast info in a callback function. However, I also want to use Skycons for visualization.

this is my forecast.js. in that script I get info like temperature etc. so I need to get "icon" data as well

const request = require('request')


const getWeather = (latitude, longitude, callback) => {

    const url = 'https://api.darksky.net/forecast/b0854aec02e1655c7203e05c7d77dfd1/' + latitude + ',' + longitude + '/?units=si'

    request({
        url: url,
        json: true
    }, (error, {
        body /* "response " evezine response object icindeki "body" birbasa daxil edirem function-a*/
    }) => {
        if (error) {
            callback('Unable to connect to weather service!', undefined)
        } else if (body.error) {
            callback('Unable to find location'.undefined)
        } else {
            callback(undefined,
                'It is currently ' + body.currently.temperature + '°C out in ' + body.timezone + '. Weather ' + body.daily.data[0].summary + ' There is a ' + (body.currently.precipProbability * 100) + '% chance of rain.'
            )
        }
    })

}

module.exports = getWeather

This is the fetch function, and I tried to invoke and activate Skycons in this function. but I cannot get "icon" data from API.

const weatherForm = document.querySelector("form");
const search = document.querySelector("input");

const messageOne = document.querySelector("#message-1");
const messageTwo = document.querySelector("#message-2");

const skycons = new Skycons({
  color: '#222'
})
skycons.set('icon', 'clear-day');
skycons.play();

const icon = data.forecast.icon;


weatherForm.addEventListener("submit", e => {
  e.preventDefault();

  const location = search.value;

  messageOne.textContent = "Please wait....";
  messageTwo.textContent = "";

  fetch(
    "http://localhost:4000/weather?address=" + encodeURIComponent(location)
  ).then(response => {
    response.json().then(data => {
      if (data.error) {
        messageOne.textContent = data.error;
      } else {
        messageOne.textContent = data.location;
        messageTwo.textContent = data.forecast;

      }
      currentSkycons(icon, document.getElementById('icon'));
    });
  });


  messageOne.textContent;
  messageTwo.textContent;
});

function currentSkycons(icon, iconID) {

  const currentIcon = icon.replace(/-/g, "_").toUppercase();
  skycons.play();
  return skycons.set(iconID, Skycons[currentIcon]);

}

but to use Skycons, I need to get "icon" from the dark-sky API. how I can get this data aside from my forecast js? To get and assign that data to a variable and use in another function

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
ilkin
  • 99
  • 1
  • 2
  • 11

1 Answers1

0

It looks like the data object is only accessible in the response json, which means you would need to access forcast.icon when you have the response.

hdennen
  • 57
  • 8
  • but my response json in another js. and whenever I try to pull forecast.icon its just gave me as a string, not a variable – ilkin Nov 07 '19 at 06:54