3

my problem probably has an incredibly easy fix, but I'm new to javascript and can't seem to find an answer for it. Heres the script:

var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
    targetUrl = 'https://api.darksky.net/forecast/[key]/[latitude],[longitude]'
fetch(proxyUrl + targetUrl)
  .then(blob => blob.json())
  .then(data => {
    console.log(data);
    document.getElementById('weather').innerHTML = data;
  })

However when i run it, the <p> element doesnt change to the data itslef, it changes to "[object Object]" What am I doing wrong? Any help is appreciated.

PS: the targetUrl variable has placeholders where the parameters go, it won't run as-is.

Wyatt Nulton
  • 129
  • 1
  • 2
  • 10

2 Answers2

5

From what I understand you are trying to print the data into <p> element with id as "weather".

Please replace following line.

document.getElementById('weather').innerHTML = JSON.stringify(data);

It will work just fine.

Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
  • 1
    [This question](https://stackoverflow.com/q/957537/5037905) may help if you're looking for different ways to inspect your object. – Eric Reed Aug 18 '19 at 19:41
1

Check the data returned by the API call and then add it to the html. I think it is returning an object and you are trying to display that object in the DOM.