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 = '/',
targetUrl = '/[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.
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./',
targetUrl = 'https://api.darksky/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.
-
data
is not a string, it is an object. Yourconsole.log(data)
will show you the contents of the object. If there's a specific property of the object you want to display, usedata.PropertyName
. – Tyler Roper Commented Aug 18, 2019 at 19:35 -
If you want to print the whole object just use:
JSON.stringify(data);
– Adrian Commented Aug 18, 2019 at 19:35 - Is "weather" I'd of the <p> element? – Sangwin Gawande Commented Aug 18, 2019 at 19:37
-
Setting the innerHTML value will call
toObject
which will print[object Object]
– evolutionxbox Commented Aug 18, 2019 at 19:37
2 Answers
Reset to default 6From 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.
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.