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










share|improve this question






















  • 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















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










share|improve this question






















  • 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













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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 18:56









Dana

22




22












  • 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


















  • 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
















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












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Have you tried data.results.forEach instead of data.forEach ?






share|improve this answer





















  • 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












  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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 ?






share|improve this answer





















  • 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












  • 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















up vote
0
down vote



accepted










Have you tried data.results.forEach instead of data.forEach ?






share|improve this answer





















  • 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












  • 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













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Have you tried data.results.forEach instead of data.forEach ?






share|improve this answer












Have you tried data.results.forEach instead of data.forEach ?







share|improve this answer












share|improve this answer



share|improve this answer










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 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












  • 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












  • 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












  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)