Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop "[object Object]" from printing rather than the object itself?

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.

like image 366
Wyatt Nulton Avatar asked Oct 19 '25 00:10

Wyatt Nulton


2 Answers

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.

like image 84
Sangwin Gawande Avatar answered Oct 20 '25 15:10

Sangwin Gawande


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.

like image 20
Praveen kumar Avatar answered Oct 20 '25 13:10

Praveen kumar