Why am I getting undefined as an answer when fetching data from an api?
up vote
0
down vote
favorite
I learning how to use fetch api and I am trying to print a quote from the Simpsons api as practice. The problem is that keep getting undefined as an answer. Do you know why it is not just printing the quote?
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
quote.innerText = data.quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
javascript ajax api fetch
add a comment |
up vote
0
down vote
favorite
I learning how to use fetch api and I am trying to print a quote from the Simpsons api as practice. The problem is that keep getting undefined as an answer. Do you know why it is not just printing the quote?
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
quote.innerText = data.quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
javascript ajax api fetch
data[0].quote
- because data in this case is an array
– Bravo
Nov 8 at 3:06
That should be an answer :P.
– zozo
Nov 8 at 3:07
2
I did, then I realised that 3 seconds of debugging is all it would take to fix such a basic issue @zozo - so I deleted the answer
– Bravo
Nov 8 at 3:20
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I learning how to use fetch api and I am trying to print a quote from the Simpsons api as practice. The problem is that keep getting undefined as an answer. Do you know why it is not just printing the quote?
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
quote.innerText = data.quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
javascript ajax api fetch
I learning how to use fetch api and I am trying to print a quote from the Simpsons api as practice. The problem is that keep getting undefined as an answer. Do you know why it is not just printing the quote?
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
quote.innerText = data.quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
javascript ajax api fetch
javascript ajax api fetch
asked Nov 8 at 3:04
bsmith
63
63
data[0].quote
- because data in this case is an array
– Bravo
Nov 8 at 3:06
That should be an answer :P.
– zozo
Nov 8 at 3:07
2
I did, then I realised that 3 seconds of debugging is all it would take to fix such a basic issue @zozo - so I deleted the answer
– Bravo
Nov 8 at 3:20
add a comment |
data[0].quote
- because data in this case is an array
– Bravo
Nov 8 at 3:06
That should be an answer :P.
– zozo
Nov 8 at 3:07
2
I did, then I realised that 3 seconds of debugging is all it would take to fix such a basic issue @zozo - so I deleted the answer
– Bravo
Nov 8 at 3:20
data[0].quote
- because data in this case is an array– Bravo
Nov 8 at 3:06
data[0].quote
- because data in this case is an array– Bravo
Nov 8 at 3:06
That should be an answer :P.
– zozo
Nov 8 at 3:07
That should be an answer :P.
– zozo
Nov 8 at 3:07
2
2
I did, then I realised that 3 seconds of debugging is all it would take to fix such a basic issue @zozo - so I deleted the answer
– Bravo
Nov 8 at 3:20
I did, then I realised that 3 seconds of debugging is all it would take to fix such a basic issue @zozo - so I deleted the answer
– Bravo
Nov 8 at 3:20
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
The data of second .then
is an array with a object inside.
If you want to get quote
in the object, you need to use data[0]
to select the object.Then, use .quote
to select the key quote
in the object. And you can get the value you want.
let button = document.querySelector('#button');
let quote = document.querySelector('#quote');
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
console.log(data);
//this is an array with object(index = 0) inside [{...}]
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch);
Thanks! That solved it!
– bsmith
Nov 8 at 16:05
add a comment |
up vote
2
down vote
The API's response appears to be an array, which means you'll need to need to access the quote data from say, the first item of the array response.
You can do that by making the following change to your code:
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
// Access first item of data "array" to retrieve and display quote
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
<button id="button">Get quote</button>
<div id="quote"></div>
add a comment |
up vote
0
down vote
Data which you are parsing is of Array type
[{"quote":"I can't even say the word 'titmouse' without gigggling like a schoolgirl.","character":"Homer Simpson","image":"https://cdn.glitch.com/3c3ffadc-3406-4440-bb95-d40ec8fcde72%2FHomerSimpson.png?1497567511939","characterDirection":"Right"}]
So to read that you need to pass index as well
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
Here is working fiddle
Thanks! It is working now!
– bsmith
Nov 8 at 16:05
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The data of second .then
is an array with a object inside.
If you want to get quote
in the object, you need to use data[0]
to select the object.Then, use .quote
to select the key quote
in the object. And you can get the value you want.
let button = document.querySelector('#button');
let quote = document.querySelector('#quote');
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
console.log(data);
//this is an array with object(index = 0) inside [{...}]
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch);
Thanks! That solved it!
– bsmith
Nov 8 at 16:05
add a comment |
up vote
0
down vote
accepted
The data of second .then
is an array with a object inside.
If you want to get quote
in the object, you need to use data[0]
to select the object.Then, use .quote
to select the key quote
in the object. And you can get the value you want.
let button = document.querySelector('#button');
let quote = document.querySelector('#quote');
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
console.log(data);
//this is an array with object(index = 0) inside [{...}]
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch);
Thanks! That solved it!
– bsmith
Nov 8 at 16:05
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The data of second .then
is an array with a object inside.
If you want to get quote
in the object, you need to use data[0]
to select the object.Then, use .quote
to select the key quote
in the object. And you can get the value you want.
let button = document.querySelector('#button');
let quote = document.querySelector('#quote');
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
console.log(data);
//this is an array with object(index = 0) inside [{...}]
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch);
The data of second .then
is an array with a object inside.
If you want to get quote
in the object, you need to use data[0]
to select the object.Then, use .quote
to select the key quote
in the object. And you can get the value you want.
let button = document.querySelector('#button');
let quote = document.querySelector('#quote');
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
console.log(data);
//this is an array with object(index = 0) inside [{...}]
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch);
edited Nov 8 at 3:25
answered Nov 8 at 3:19
Jason Yang
161
161
Thanks! That solved it!
– bsmith
Nov 8 at 16:05
add a comment |
Thanks! That solved it!
– bsmith
Nov 8 at 16:05
Thanks! That solved it!
– bsmith
Nov 8 at 16:05
Thanks! That solved it!
– bsmith
Nov 8 at 16:05
add a comment |
up vote
2
down vote
The API's response appears to be an array, which means you'll need to need to access the quote data from say, the first item of the array response.
You can do that by making the following change to your code:
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
// Access first item of data "array" to retrieve and display quote
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
<button id="button">Get quote</button>
<div id="quote"></div>
add a comment |
up vote
2
down vote
The API's response appears to be an array, which means you'll need to need to access the quote data from say, the first item of the array response.
You can do that by making the following change to your code:
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
// Access first item of data "array" to retrieve and display quote
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
<button id="button">Get quote</button>
<div id="quote"></div>
add a comment |
up vote
2
down vote
up vote
2
down vote
The API's response appears to be an array, which means you'll need to need to access the quote data from say, the first item of the array response.
You can do that by making the following change to your code:
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
// Access first item of data "array" to retrieve and display quote
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
<button id="button">Get quote</button>
<div id="quote"></div>
The API's response appears to be an array, which means you'll need to need to access the quote data from say, the first item of the array response.
You can do that by making the following change to your code:
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
// Access first item of data "array" to retrieve and display quote
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
<button id="button">Get quote</button>
<div id="quote"></div>
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
// Access first item of data "array" to retrieve and display quote
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
<button id="button">Get quote</button>
<div id="quote"></div>
let button = document.querySelector('#button')
let quote = document.querySelector('#quote')
function getInfoFetch(){
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
// Access first item of data "array" to retrieve and display quote
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
}
button.addEventListener('click', getInfoFetch)
<button id="button">Get quote</button>
<div id="quote"></div>
edited Nov 8 at 9:26
answered Nov 8 at 3:08
Dacre Denny
8,9724729
8,9724729
add a comment |
add a comment |
up vote
0
down vote
Data which you are parsing is of Array type
[{"quote":"I can't even say the word 'titmouse' without gigggling like a schoolgirl.","character":"Homer Simpson","image":"https://cdn.glitch.com/3c3ffadc-3406-4440-bb95-d40ec8fcde72%2FHomerSimpson.png?1497567511939","characterDirection":"Right"}]
So to read that you need to pass index as well
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
Here is working fiddle
Thanks! It is working now!
– bsmith
Nov 8 at 16:05
add a comment |
up vote
0
down vote
Data which you are parsing is of Array type
[{"quote":"I can't even say the word 'titmouse' without gigggling like a schoolgirl.","character":"Homer Simpson","image":"https://cdn.glitch.com/3c3ffadc-3406-4440-bb95-d40ec8fcde72%2FHomerSimpson.png?1497567511939","characterDirection":"Right"}]
So to read that you need to pass index as well
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
Here is working fiddle
Thanks! It is working now!
– bsmith
Nov 8 at 16:05
add a comment |
up vote
0
down vote
up vote
0
down vote
Data which you are parsing is of Array type
[{"quote":"I can't even say the word 'titmouse' without gigggling like a schoolgirl.","character":"Homer Simpson","image":"https://cdn.glitch.com/3c3ffadc-3406-4440-bb95-d40ec8fcde72%2FHomerSimpson.png?1497567511939","characterDirection":"Right"}]
So to read that you need to pass index as well
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
Here is working fiddle
Data which you are parsing is of Array type
[{"quote":"I can't even say the word 'titmouse' without gigggling like a schoolgirl.","character":"Homer Simpson","image":"https://cdn.glitch.com/3c3ffadc-3406-4440-bb95-d40ec8fcde72%2FHomerSimpson.png?1497567511939","characterDirection":"Right"}]
So to read that you need to pass index as well
fetch('https://thesimpsonsquoteapi.glitch.me/quotes')
.then(response => response.json())
.then((data) => {
quote.innerText = data[0].quote;
})
.catch(err => console.log(err));
Here is working fiddle
edited Nov 8 at 3:24
answered Nov 8 at 3:08
MyTwoCents
2,8211827
2,8211827
Thanks! It is working now!
– bsmith
Nov 8 at 16:05
add a comment |
Thanks! It is working now!
– bsmith
Nov 8 at 16:05
Thanks! It is working now!
– bsmith
Nov 8 at 16:05
Thanks! It is working now!
– bsmith
Nov 8 at 16:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200964%2fwhy-am-i-getting-undefined-as-an-answer-when-fetching-data-from-an-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
data[0].quote
- because data in this case is an array– Bravo
Nov 8 at 3:06
That should be an answer :P.
– zozo
Nov 8 at 3:07
2
I did, then I realised that 3 seconds of debugging is all it would take to fix such a basic issue @zozo - so I deleted the answer
– Bravo
Nov 8 at 3:20