How do I fix my JavaScript API error message: Uncaught Typeerror: data.forEach is not a function?
up vote
-1
down vote
favorite
I am using JavaScript to make an API call and receiving the "Uncaught TypeError: data.forEach is not a function", error. I have researched this error at length and either the fixes I tried did not work or I was left saying, "What the. . .?" I am a new developer and still trying to understand error message terminology. Here is what I have tried:
1.trying "onreadystatechange" instead of onload
2. clear cache and cookies
3. different browser
4. look for syntax errors
5. for in loop
**Here is my JavaScript API code:*
const app = document.getElementById('root');
const image =document.createElement('img');
image.src= 'woman_pc.jpg';
const container = document.createElement('div');
container.setAttribute('class', 'container');
app.appendChild(image);
app.appendChild(container);
var request = new XMLHttpRequest();
request.open('GET', 'https://www.themuse.com/api/public/jobs?page=1',
true);
request.onload = function () {
// Begin accessing JSON data here
var data = JSON.parse(this.response);
**if (request.status >= 200 && request.status < 400) {
data.forEach (job => {
const card = document.createElement('div');
card.setAttribute('class', 'card');**
const h1 = document.createElement('h1');
h1.textContent = job.title;
const p = document.createElement('p');
job.description = job.description.substring(0, 300);
p.textContent = `${job.description}...`;
container.appendChild(card);
card.appendChild(h1);
card.appendChild(p);
});
} else {
const errorMessage = document.createElement('marquee');
errorMessage.textContent = `Darn, it's not working!`;
app.appendChild(errorMessage);
}
}
request.send();
Any guidance would be greatly appreciated!
Best,
Dana
javascript api error-handling
add a comment |
up vote
-1
down vote
favorite
I am using JavaScript to make an API call and receiving the "Uncaught TypeError: data.forEach is not a function", error. I have researched this error at length and either the fixes I tried did not work or I was left saying, "What the. . .?" I am a new developer and still trying to understand error message terminology. Here is what I have tried:
1.trying "onreadystatechange" instead of onload
2. clear cache and cookies
3. different browser
4. look for syntax errors
5. for in loop
**Here is my JavaScript API code:*
const app = document.getElementById('root');
const image =document.createElement('img');
image.src= 'woman_pc.jpg';
const container = document.createElement('div');
container.setAttribute('class', 'container');
app.appendChild(image);
app.appendChild(container);
var request = new XMLHttpRequest();
request.open('GET', 'https://www.themuse.com/api/public/jobs?page=1',
true);
request.onload = function () {
// Begin accessing JSON data here
var data = JSON.parse(this.response);
**if (request.status >= 200 && request.status < 400) {
data.forEach (job => {
const card = document.createElement('div');
card.setAttribute('class', 'card');**
const h1 = document.createElement('h1');
h1.textContent = job.title;
const p = document.createElement('p');
job.description = job.description.substring(0, 300);
p.textContent = `${job.description}...`;
container.appendChild(card);
card.appendChild(h1);
card.appendChild(p);
});
} else {
const errorMessage = document.createElement('marquee');
errorMessage.textContent = `Darn, it's not working!`;
app.appendChild(errorMessage);
}
}
request.send();
Any guidance would be greatly appreciated!
Best,
Dana
javascript api error-handling
var data = JSON.parse(this.response);this is an Object,forEachis for Arrays
– darklightcode
Nov 19 at 18:58
console.log(data)is it what you expect?
– epascarello
Nov 19 at 18:59
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am using JavaScript to make an API call and receiving the "Uncaught TypeError: data.forEach is not a function", error. I have researched this error at length and either the fixes I tried did not work or I was left saying, "What the. . .?" I am a new developer and still trying to understand error message terminology. Here is what I have tried:
1.trying "onreadystatechange" instead of onload
2. clear cache and cookies
3. different browser
4. look for syntax errors
5. for in loop
**Here is my JavaScript API code:*
const app = document.getElementById('root');
const image =document.createElement('img');
image.src= 'woman_pc.jpg';
const container = document.createElement('div');
container.setAttribute('class', 'container');
app.appendChild(image);
app.appendChild(container);
var request = new XMLHttpRequest();
request.open('GET', 'https://www.themuse.com/api/public/jobs?page=1',
true);
request.onload = function () {
// Begin accessing JSON data here
var data = JSON.parse(this.response);
**if (request.status >= 200 && request.status < 400) {
data.forEach (job => {
const card = document.createElement('div');
card.setAttribute('class', 'card');**
const h1 = document.createElement('h1');
h1.textContent = job.title;
const p = document.createElement('p');
job.description = job.description.substring(0, 300);
p.textContent = `${job.description}...`;
container.appendChild(card);
card.appendChild(h1);
card.appendChild(p);
});
} else {
const errorMessage = document.createElement('marquee');
errorMessage.textContent = `Darn, it's not working!`;
app.appendChild(errorMessage);
}
}
request.send();
Any guidance would be greatly appreciated!
Best,
Dana
javascript api error-handling
I am using JavaScript to make an API call and receiving the "Uncaught TypeError: data.forEach is not a function", error. I have researched this error at length and either the fixes I tried did not work or I was left saying, "What the. . .?" I am a new developer and still trying to understand error message terminology. Here is what I have tried:
1.trying "onreadystatechange" instead of onload
2. clear cache and cookies
3. different browser
4. look for syntax errors
5. for in loop
**Here is my JavaScript API code:*
const app = document.getElementById('root');
const image =document.createElement('img');
image.src= 'woman_pc.jpg';
const container = document.createElement('div');
container.setAttribute('class', 'container');
app.appendChild(image);
app.appendChild(container);
var request = new XMLHttpRequest();
request.open('GET', 'https://www.themuse.com/api/public/jobs?page=1',
true);
request.onload = function () {
// Begin accessing JSON data here
var data = JSON.parse(this.response);
**if (request.status >= 200 && request.status < 400) {
data.forEach (job => {
const card = document.createElement('div');
card.setAttribute('class', 'card');**
const h1 = document.createElement('h1');
h1.textContent = job.title;
const p = document.createElement('p');
job.description = job.description.substring(0, 300);
p.textContent = `${job.description}...`;
container.appendChild(card);
card.appendChild(h1);
card.appendChild(p);
});
} else {
const errorMessage = document.createElement('marquee');
errorMessage.textContent = `Darn, it's not working!`;
app.appendChild(errorMessage);
}
}
request.send();
Any guidance would be greatly appreciated!
Best,
Dana
javascript api error-handling
javascript api error-handling
asked Nov 19 at 18:56
Dana
22
22
var data = JSON.parse(this.response);this is an Object,forEachis for Arrays
– darklightcode
Nov 19 at 18:58
console.log(data)is it what you expect?
– epascarello
Nov 19 at 18:59
add a comment |
var data = JSON.parse(this.response);this is an Object,forEachis for Arrays
– darklightcode
Nov 19 at 18:58
console.log(data)is it what you expect?
– epascarello
Nov 19 at 18:59
var data = JSON.parse(this.response); this is an Object, forEach is for Arrays– darklightcode
Nov 19 at 18:58
var data = JSON.parse(this.response); this is an Object, forEach is for Arrays– darklightcode
Nov 19 at 18:58
console.log(data) is it what you expect?– epascarello
Nov 19 at 18:59
console.log(data) is it what you expect?– epascarello
Nov 19 at 18:59
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Have you tried data.results.forEach instead of data.forEach ?
I just tried that and it returned this error: Uncaught TypeError: Cannot read property 'substring' of undefined at data.results.forEach.job (script.js:27) at Array.forEach (<anonymous>) at XMLHttpRequest.request.onload (script.js:19)
– Dana
Nov 19 at 21:32
Well, that is another error not related to thedata.results.forEachbut to the code inside which. Can you tell what code is at line 27?
– TeeKea
Nov 19 at 22:34
Line 26, 27 & 28: const p = document.createElement('p'); job.description = job.description.substring(0, 300); p.textContent =${job.description}...;
– Dana
Nov 20 at 1:09
Try:job.description = JSON.stringify(job.description).substring(0, 300);
– TeeKea
Nov 20 at 5:45
That worked! I am very grateful. Thanks so much. Have a great week!
– Dana
Nov 20 at 13:13
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Have you tried data.results.forEach instead of data.forEach ?
I just tried that and it returned this error: Uncaught TypeError: Cannot read property 'substring' of undefined at data.results.forEach.job (script.js:27) at Array.forEach (<anonymous>) at XMLHttpRequest.request.onload (script.js:19)
– Dana
Nov 19 at 21:32
Well, that is another error not related to thedata.results.forEachbut to the code inside which. Can you tell what code is at line 27?
– TeeKea
Nov 19 at 22:34
Line 26, 27 & 28: const p = document.createElement('p'); job.description = job.description.substring(0, 300); p.textContent =${job.description}...;
– Dana
Nov 20 at 1:09
Try:job.description = JSON.stringify(job.description).substring(0, 300);
– TeeKea
Nov 20 at 5:45
That worked! I am very grateful. Thanks so much. Have a great week!
– Dana
Nov 20 at 13:13
|
show 1 more comment
up vote
0
down vote
accepted
Have you tried data.results.forEach instead of data.forEach ?
I just tried that and it returned this error: Uncaught TypeError: Cannot read property 'substring' of undefined at data.results.forEach.job (script.js:27) at Array.forEach (<anonymous>) at XMLHttpRequest.request.onload (script.js:19)
– Dana
Nov 19 at 21:32
Well, that is another error not related to thedata.results.forEachbut to the code inside which. Can you tell what code is at line 27?
– TeeKea
Nov 19 at 22:34
Line 26, 27 & 28: const p = document.createElement('p'); job.description = job.description.substring(0, 300); p.textContent =${job.description}...;
– Dana
Nov 20 at 1:09
Try:job.description = JSON.stringify(job.description).substring(0, 300);
– TeeKea
Nov 20 at 5:45
That worked! I am very grateful. Thanks so much. Have a great week!
– Dana
Nov 20 at 13:13
|
show 1 more comment
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Have you tried data.results.forEach instead of data.forEach ?
Have you tried data.results.forEach instead of data.forEach ?
answered Nov 19 at 19:01
TeeKea
1,88011023
1,88011023
I just tried that and it returned this error: Uncaught TypeError: Cannot read property 'substring' of undefined at data.results.forEach.job (script.js:27) at Array.forEach (<anonymous>) at XMLHttpRequest.request.onload (script.js:19)
– Dana
Nov 19 at 21:32
Well, that is another error not related to thedata.results.forEachbut to the code inside which. Can you tell what code is at line 27?
– TeeKea
Nov 19 at 22:34
Line 26, 27 & 28: const p = document.createElement('p'); job.description = job.description.substring(0, 300); p.textContent =${job.description}...;
– Dana
Nov 20 at 1:09
Try:job.description = JSON.stringify(job.description).substring(0, 300);
– TeeKea
Nov 20 at 5:45
That worked! I am very grateful. Thanks so much. Have a great week!
– Dana
Nov 20 at 13:13
|
show 1 more comment
I just tried that and it returned this error: Uncaught TypeError: Cannot read property 'substring' of undefined at data.results.forEach.job (script.js:27) at Array.forEach (<anonymous>) at XMLHttpRequest.request.onload (script.js:19)
– Dana
Nov 19 at 21:32
Well, that is another error not related to thedata.results.forEachbut to the code inside which. Can you tell what code is at line 27?
– TeeKea
Nov 19 at 22:34
Line 26, 27 & 28: const p = document.createElement('p'); job.description = job.description.substring(0, 300); p.textContent =${job.description}...;
– Dana
Nov 20 at 1:09
Try:job.description = JSON.stringify(job.description).substring(0, 300);
– TeeKea
Nov 20 at 5:45
That worked! I am very grateful. Thanks so much. Have a great week!
– Dana
Nov 20 at 13:13
I just tried that and it returned this error: Uncaught TypeError: Cannot read property 'substring' of undefined at data.results.forEach.job (script.js:27) at Array.forEach (<anonymous>) at XMLHttpRequest.request.onload (script.js:19)
– Dana
Nov 19 at 21:32
I just tried that and it returned this error: Uncaught TypeError: Cannot read property 'substring' of undefined at data.results.forEach.job (script.js:27) at Array.forEach (<anonymous>) at XMLHttpRequest.request.onload (script.js:19)
– Dana
Nov 19 at 21:32
Well, that is another error not related to the
data.results.forEach but to the code inside which. Can you tell what code is at line 27?– TeeKea
Nov 19 at 22:34
Well, that is another error not related to the
data.results.forEach but to the code inside which. Can you tell what code is at line 27?– TeeKea
Nov 19 at 22:34
Line 26, 27 & 28: const p = document.createElement('p'); job.description = job.description.substring(0, 300); p.textContent =
${job.description}...;– Dana
Nov 20 at 1:09
Line 26, 27 & 28: const p = document.createElement('p'); job.description = job.description.substring(0, 300); p.textContent =
${job.description}...;– Dana
Nov 20 at 1:09
Try:
job.description = JSON.stringify(job.description).substring(0, 300);– TeeKea
Nov 20 at 5:45
Try:
job.description = JSON.stringify(job.description).substring(0, 300);– TeeKea
Nov 20 at 5:45
That worked! I am very grateful. Thanks so much. Have a great week!
– Dana
Nov 20 at 13:13
That worked! I am very grateful. Thanks so much. Have a great week!
– Dana
Nov 20 at 13:13
|
show 1 more 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%2f53380981%2fhow-do-i-fix-my-javascript-api-error-message-uncaught-typeerror-data-foreach-i%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
var data = JSON.parse(this.response);this is an Object,forEachis for Arrays– darklightcode
Nov 19 at 18:58
console.log(data)is it what you expect?– epascarello
Nov 19 at 18:59