How to fix ‘Cannot read property 'forEach' of undefined’ error in Angular
I have a bug, but I don't know what else to do to fix it. Below is my code and the error:
courses: any = ;
ngOnInit() {
this.locale = this.localeService.currentLocale();
this.user = this.authService.getCurrentUser();
this.userService.getCoursesInProgress(this.user.id).subscribe(courses => {
this.coursesInProgress = ;
courses.forEach(n => {
const courseObj: Course = new Course(n);
this.coursesInProgress.push(courseObj);
});
});
this.userService.getCoursesFinished(this.user.id).subscribe(courses => {
this.coursesFinished = ;
courses.forEach(n => {
const courseObj: Course = new Course(n);
this.coursesFinished.push(courseObj);
});
});
}
TypeError · Cannot read property 'forEach' of undefined
angular typescript angular7
|
show 6 more comments
I have a bug, but I don't know what else to do to fix it. Below is my code and the error:
courses: any = ;
ngOnInit() {
this.locale = this.localeService.currentLocale();
this.user = this.authService.getCurrentUser();
this.userService.getCoursesInProgress(this.user.id).subscribe(courses => {
this.coursesInProgress = ;
courses.forEach(n => {
const courseObj: Course = new Course(n);
this.coursesInProgress.push(courseObj);
});
});
this.userService.getCoursesFinished(this.user.id).subscribe(courses => {
this.coursesFinished = ;
courses.forEach(n => {
const courseObj: Course = new Course(n);
this.coursesFinished.push(courseObj);
});
});
}
TypeError · Cannot read property 'forEach' of undefined
angular typescript angular7
is the courses array defined? It doesn't look like its defined.
– digital-pollution
Jan 2 at 10:38
3
The API is returningcourses
asundefined
ornull
. Please see the browser's network tab to check what its returning. Alternatively, you can useconsole.log(courses)
to see whats returned.
– Faisal
Jan 2 at 10:39
Sidenote, you could writethis.coursesInProgress = courses.map(n => new Course(n))
– Jeremy Thille
Jan 2 at 10:42
1
The error tells you that you're trying to useforEach
onundefined
(and contains the line number where the problem is). Your code usesforEach
oncourses
. So that means thatcourses
is undefined.courses
is the event emitted by the observable returned by one of your service method calls (the line number indicates which one it is). So that means that the service doesn't return an observable emitting an array of courses as you think, but instead emitsundefined
. You haven't posted the service, so we can't help more.
– JB Nizet
Jan 2 at 10:42
1
See this:stackoverflow.com/questions/43724426/…
– i_th
Jan 2 at 10:46
|
show 6 more comments
I have a bug, but I don't know what else to do to fix it. Below is my code and the error:
courses: any = ;
ngOnInit() {
this.locale = this.localeService.currentLocale();
this.user = this.authService.getCurrentUser();
this.userService.getCoursesInProgress(this.user.id).subscribe(courses => {
this.coursesInProgress = ;
courses.forEach(n => {
const courseObj: Course = new Course(n);
this.coursesInProgress.push(courseObj);
});
});
this.userService.getCoursesFinished(this.user.id).subscribe(courses => {
this.coursesFinished = ;
courses.forEach(n => {
const courseObj: Course = new Course(n);
this.coursesFinished.push(courseObj);
});
});
}
TypeError · Cannot read property 'forEach' of undefined
angular typescript angular7
I have a bug, but I don't know what else to do to fix it. Below is my code and the error:
courses: any = ;
ngOnInit() {
this.locale = this.localeService.currentLocale();
this.user = this.authService.getCurrentUser();
this.userService.getCoursesInProgress(this.user.id).subscribe(courses => {
this.coursesInProgress = ;
courses.forEach(n => {
const courseObj: Course = new Course(n);
this.coursesInProgress.push(courseObj);
});
});
this.userService.getCoursesFinished(this.user.id).subscribe(courses => {
this.coursesFinished = ;
courses.forEach(n => {
const courseObj: Course = new Course(n);
this.coursesFinished.push(courseObj);
});
});
}
TypeError · Cannot read property 'forEach' of undefined
angular typescript angular7
angular typescript angular7
edited Jan 2 at 11:04
veben
2,16041329
2,16041329
asked Jan 2 at 10:35
Alexandre MisaiphonAlexandre Misaiphon
1
1
is the courses array defined? It doesn't look like its defined.
– digital-pollution
Jan 2 at 10:38
3
The API is returningcourses
asundefined
ornull
. Please see the browser's network tab to check what its returning. Alternatively, you can useconsole.log(courses)
to see whats returned.
– Faisal
Jan 2 at 10:39
Sidenote, you could writethis.coursesInProgress = courses.map(n => new Course(n))
– Jeremy Thille
Jan 2 at 10:42
1
The error tells you that you're trying to useforEach
onundefined
(and contains the line number where the problem is). Your code usesforEach
oncourses
. So that means thatcourses
is undefined.courses
is the event emitted by the observable returned by one of your service method calls (the line number indicates which one it is). So that means that the service doesn't return an observable emitting an array of courses as you think, but instead emitsundefined
. You haven't posted the service, so we can't help more.
– JB Nizet
Jan 2 at 10:42
1
See this:stackoverflow.com/questions/43724426/…
– i_th
Jan 2 at 10:46
|
show 6 more comments
is the courses array defined? It doesn't look like its defined.
– digital-pollution
Jan 2 at 10:38
3
The API is returningcourses
asundefined
ornull
. Please see the browser's network tab to check what its returning. Alternatively, you can useconsole.log(courses)
to see whats returned.
– Faisal
Jan 2 at 10:39
Sidenote, you could writethis.coursesInProgress = courses.map(n => new Course(n))
– Jeremy Thille
Jan 2 at 10:42
1
The error tells you that you're trying to useforEach
onundefined
(and contains the line number where the problem is). Your code usesforEach
oncourses
. So that means thatcourses
is undefined.courses
is the event emitted by the observable returned by one of your service method calls (the line number indicates which one it is). So that means that the service doesn't return an observable emitting an array of courses as you think, but instead emitsundefined
. You haven't posted the service, so we can't help more.
– JB Nizet
Jan 2 at 10:42
1
See this:stackoverflow.com/questions/43724426/…
– i_th
Jan 2 at 10:46
is the courses array defined? It doesn't look like its defined.
– digital-pollution
Jan 2 at 10:38
is the courses array defined? It doesn't look like its defined.
– digital-pollution
Jan 2 at 10:38
3
3
The API is returning
courses
as undefined
or null
. Please see the browser's network tab to check what its returning. Alternatively, you can use console.log(courses)
to see whats returned.– Faisal
Jan 2 at 10:39
The API is returning
courses
as undefined
or null
. Please see the browser's network tab to check what its returning. Alternatively, you can use console.log(courses)
to see whats returned.– Faisal
Jan 2 at 10:39
Sidenote, you could write
this.coursesInProgress = courses.map(n => new Course(n))
– Jeremy Thille
Jan 2 at 10:42
Sidenote, you could write
this.coursesInProgress = courses.map(n => new Course(n))
– Jeremy Thille
Jan 2 at 10:42
1
1
The error tells you that you're trying to use
forEach
on undefined
(and contains the line number where the problem is). Your code uses forEach
on courses
. So that means that courses
is undefined. courses
is the event emitted by the observable returned by one of your service method calls (the line number indicates which one it is). So that means that the service doesn't return an observable emitting an array of courses as you think, but instead emits undefined
. You haven't posted the service, so we can't help more.– JB Nizet
Jan 2 at 10:42
The error tells you that you're trying to use
forEach
on undefined
(and contains the line number where the problem is). Your code uses forEach
on courses
. So that means that courses
is undefined. courses
is the event emitted by the observable returned by one of your service method calls (the line number indicates which one it is). So that means that the service doesn't return an observable emitting an array of courses as you think, but instead emits undefined
. You haven't posted the service, so we can't help more.– JB Nizet
Jan 2 at 10:42
1
1
See this:stackoverflow.com/questions/43724426/…
– i_th
Jan 2 at 10:46
See this:stackoverflow.com/questions/43724426/…
– i_th
Jan 2 at 10:46
|
show 6 more comments
0
active
oldest
votes
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f54004782%2fhow-to-fix-cannot-read-property-foreach-of-undefined-error-in-angular%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f54004782%2fhow-to-fix-cannot-read-property-foreach-of-undefined-error-in-angular%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
is the courses array defined? It doesn't look like its defined.
– digital-pollution
Jan 2 at 10:38
3
The API is returning
courses
asundefined
ornull
. Please see the browser's network tab to check what its returning. Alternatively, you can useconsole.log(courses)
to see whats returned.– Faisal
Jan 2 at 10:39
Sidenote, you could write
this.coursesInProgress = courses.map(n => new Course(n))
– Jeremy Thille
Jan 2 at 10:42
1
The error tells you that you're trying to use
forEach
onundefined
(and contains the line number where the problem is). Your code usesforEach
oncourses
. So that means thatcourses
is undefined.courses
is the event emitted by the observable returned by one of your service method calls (the line number indicates which one it is). So that means that the service doesn't return an observable emitting an array of courses as you think, but instead emitsundefined
. You haven't posted the service, so we can't help more.– JB Nizet
Jan 2 at 10:42
1
See this:stackoverflow.com/questions/43724426/…
– i_th
Jan 2 at 10:46