Getting “Cannot read property 'http' of undefined” with Angular 7
I'm working on an Angular 7 project, and I'm facing a weird problem that took me some considerable time to identify, but I don't know why this is happening and I hope you guys can help me. I created a service using angular-cli, and then I implemented it as follows:
import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {forkJoin, Observable} from "rxjs";
import {map} from "rxjs/operators";
@Injectable({
providedIn: 'root'
})
export class SampleService {
constructor(private http: HttpClient) {
}
save(sample: Sample): Observable {
return this.http.post("http://localhost:3000/samples", sample).pipe(map(this.parser));
}
saveAll(samples: Sample): Observable {
return forkJoin(samples.map(this.save))
}
private parser = (value): Sample => new Sample(value.name);
}
export class Sample {
name: string;
constructor(name: string) {
this.name = name;
}
}
I put a breakpoint on each method. When I run it I get the following:
As you can see, everything seems ok in the first breakpoint, so let's go to the next.
Now everything is undefined, and I get the following error in the console:
ERROR TypeError: Cannot read property 'http' of undefined
If I change this line:
forkJoin(samples.map(this.save))
to
forkJoin(samples.map(sample => this.save(sample)))
When I rerun the code. I get:
And now everything seems to be ok, and the code works just fine. As a java developer, in a similar situation, the first way is comparable to a method reference and it would work just fine, why in TypeScript it doesn't?
angular typescript rxjs
add a comment |
I'm working on an Angular 7 project, and I'm facing a weird problem that took me some considerable time to identify, but I don't know why this is happening and I hope you guys can help me. I created a service using angular-cli, and then I implemented it as follows:
import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {forkJoin, Observable} from "rxjs";
import {map} from "rxjs/operators";
@Injectable({
providedIn: 'root'
})
export class SampleService {
constructor(private http: HttpClient) {
}
save(sample: Sample): Observable {
return this.http.post("http://localhost:3000/samples", sample).pipe(map(this.parser));
}
saveAll(samples: Sample): Observable {
return forkJoin(samples.map(this.save))
}
private parser = (value): Sample => new Sample(value.name);
}
export class Sample {
name: string;
constructor(name: string) {
this.name = name;
}
}
I put a breakpoint on each method. When I run it I get the following:
As you can see, everything seems ok in the first breakpoint, so let's go to the next.
Now everything is undefined, and I get the following error in the console:
ERROR TypeError: Cannot read property 'http' of undefined
If I change this line:
forkJoin(samples.map(this.save))
to
forkJoin(samples.map(sample => this.save(sample)))
When I rerun the code. I get:
And now everything seems to be ok, and the code works just fine. As a java developer, in a similar situation, the first way is comparable to a method reference and it would work just fine, why in TypeScript it doesn't?
angular typescript rxjs
1
this is how javascript works. If you want to keep the first option then write it as instance function github.com/Microsoft/TypeScript/wiki/…
– yurzui
Dec 27 '18 at 19:53
1
Its related to the context of object. In first case context will change however in second option context will always be the same. Refer- ryanmorr.com/understanding-scope-and-context-in-javascript
– Sunil Singh
Dec 27 '18 at 20:00
Possible duplicate of How to access the correct `this` inside a callback?
– JB Nizet
Dec 27 '18 at 20:10
return forkJoin(samples.map(sample => this.save(sample)));
– JB Nizet
Dec 27 '18 at 20:11
add a comment |
I'm working on an Angular 7 project, and I'm facing a weird problem that took me some considerable time to identify, but I don't know why this is happening and I hope you guys can help me. I created a service using angular-cli, and then I implemented it as follows:
import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {forkJoin, Observable} from "rxjs";
import {map} from "rxjs/operators";
@Injectable({
providedIn: 'root'
})
export class SampleService {
constructor(private http: HttpClient) {
}
save(sample: Sample): Observable {
return this.http.post("http://localhost:3000/samples", sample).pipe(map(this.parser));
}
saveAll(samples: Sample): Observable {
return forkJoin(samples.map(this.save))
}
private parser = (value): Sample => new Sample(value.name);
}
export class Sample {
name: string;
constructor(name: string) {
this.name = name;
}
}
I put a breakpoint on each method. When I run it I get the following:
As you can see, everything seems ok in the first breakpoint, so let's go to the next.
Now everything is undefined, and I get the following error in the console:
ERROR TypeError: Cannot read property 'http' of undefined
If I change this line:
forkJoin(samples.map(this.save))
to
forkJoin(samples.map(sample => this.save(sample)))
When I rerun the code. I get:
And now everything seems to be ok, and the code works just fine. As a java developer, in a similar situation, the first way is comparable to a method reference and it would work just fine, why in TypeScript it doesn't?
angular typescript rxjs
I'm working on an Angular 7 project, and I'm facing a weird problem that took me some considerable time to identify, but I don't know why this is happening and I hope you guys can help me. I created a service using angular-cli, and then I implemented it as follows:
import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {forkJoin, Observable} from "rxjs";
import {map} from "rxjs/operators";
@Injectable({
providedIn: 'root'
})
export class SampleService {
constructor(private http: HttpClient) {
}
save(sample: Sample): Observable {
return this.http.post("http://localhost:3000/samples", sample).pipe(map(this.parser));
}
saveAll(samples: Sample): Observable {
return forkJoin(samples.map(this.save))
}
private parser = (value): Sample => new Sample(value.name);
}
export class Sample {
name: string;
constructor(name: string) {
this.name = name;
}
}
I put a breakpoint on each method. When I run it I get the following:
As you can see, everything seems ok in the first breakpoint, so let's go to the next.
Now everything is undefined, and I get the following error in the console:
ERROR TypeError: Cannot read property 'http' of undefined
If I change this line:
forkJoin(samples.map(this.save))
to
forkJoin(samples.map(sample => this.save(sample)))
When I rerun the code. I get:
And now everything seems to be ok, and the code works just fine. As a java developer, in a similar situation, the first way is comparable to a method reference and it would work just fine, why in TypeScript it doesn't?
angular typescript rxjs
angular typescript rxjs
asked Dec 27 '18 at 19:49
Felipe Belluco
73069
73069
1
this is how javascript works. If you want to keep the first option then write it as instance function github.com/Microsoft/TypeScript/wiki/…
– yurzui
Dec 27 '18 at 19:53
1
Its related to the context of object. In first case context will change however in second option context will always be the same. Refer- ryanmorr.com/understanding-scope-and-context-in-javascript
– Sunil Singh
Dec 27 '18 at 20:00
Possible duplicate of How to access the correct `this` inside a callback?
– JB Nizet
Dec 27 '18 at 20:10
return forkJoin(samples.map(sample => this.save(sample)));
– JB Nizet
Dec 27 '18 at 20:11
add a comment |
1
this is how javascript works. If you want to keep the first option then write it as instance function github.com/Microsoft/TypeScript/wiki/…
– yurzui
Dec 27 '18 at 19:53
1
Its related to the context of object. In first case context will change however in second option context will always be the same. Refer- ryanmorr.com/understanding-scope-and-context-in-javascript
– Sunil Singh
Dec 27 '18 at 20:00
Possible duplicate of How to access the correct `this` inside a callback?
– JB Nizet
Dec 27 '18 at 20:10
return forkJoin(samples.map(sample => this.save(sample)));
– JB Nizet
Dec 27 '18 at 20:11
1
1
this is how javascript works. If you want to keep the first option then write it as instance function github.com/Microsoft/TypeScript/wiki/…
– yurzui
Dec 27 '18 at 19:53
this is how javascript works. If you want to keep the first option then write it as instance function github.com/Microsoft/TypeScript/wiki/…
– yurzui
Dec 27 '18 at 19:53
1
1
Its related to the context of object. In first case context will change however in second option context will always be the same. Refer- ryanmorr.com/understanding-scope-and-context-in-javascript
– Sunil Singh
Dec 27 '18 at 20:00
Its related to the context of object. In first case context will change however in second option context will always be the same. Refer- ryanmorr.com/understanding-scope-and-context-in-javascript
– Sunil Singh
Dec 27 '18 at 20:00
Possible duplicate of How to access the correct `this` inside a callback?
– JB Nizet
Dec 27 '18 at 20:10
Possible duplicate of How to access the correct `this` inside a callback?
– JB Nizet
Dec 27 '18 at 20:10
return forkJoin(samples.map(sample => this.save(sample)));
– JB Nizet
Dec 27 '18 at 20:11
return forkJoin(samples.map(sample => this.save(sample)));
– JB Nizet
Dec 27 '18 at 20:11
add a comment |
1 Answer
1
active
oldest
votes
this
is contextual. Every function defined using function
definition gets its own this
.
Arrow function definition works differently in that, it binds the parent context to the function body, making this
refer to the parent this
. It is similar to defining a function and binding it explicitly like:
function(doc){
//function body
}).bind(this)
add a comment |
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%2f53950164%2fgetting-cannot-read-property-http-of-undefined-with-angular-7%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
this
is contextual. Every function defined using function
definition gets its own this
.
Arrow function definition works differently in that, it binds the parent context to the function body, making this
refer to the parent this
. It is similar to defining a function and binding it explicitly like:
function(doc){
//function body
}).bind(this)
add a comment |
this
is contextual. Every function defined using function
definition gets its own this
.
Arrow function definition works differently in that, it binds the parent context to the function body, making this
refer to the parent this
. It is similar to defining a function and binding it explicitly like:
function(doc){
//function body
}).bind(this)
add a comment |
this
is contextual. Every function defined using function
definition gets its own this
.
Arrow function definition works differently in that, it binds the parent context to the function body, making this
refer to the parent this
. It is similar to defining a function and binding it explicitly like:
function(doc){
//function body
}).bind(this)
this
is contextual. Every function defined using function
definition gets its own this
.
Arrow function definition works differently in that, it binds the parent context to the function body, making this
refer to the parent this
. It is similar to defining a function and binding it explicitly like:
function(doc){
//function body
}).bind(this)
answered Dec 27 '18 at 20:42
Aragorn
1,95011226
1,95011226
add a comment |
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%2f53950164%2fgetting-cannot-read-property-http-of-undefined-with-angular-7%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
1
this is how javascript works. If you want to keep the first option then write it as instance function github.com/Microsoft/TypeScript/wiki/…
– yurzui
Dec 27 '18 at 19:53
1
Its related to the context of object. In first case context will change however in second option context will always be the same. Refer- ryanmorr.com/understanding-scope-and-context-in-javascript
– Sunil Singh
Dec 27 '18 at 20:00
Possible duplicate of How to access the correct `this` inside a callback?
– JB Nizet
Dec 27 '18 at 20:10
return forkJoin(samples.map(sample => this.save(sample)));
– JB Nizet
Dec 27 '18 at 20:11