Angular EventEmitter returning undefined object
I'm working on a project for school and I've hit a wall with my inexperience with Angular. I've got a left-nav component with a bunch of checkbox selections, and when a user selects one of the "codes" an API call goes off to our data store to retrieve all the values for a given "key"
this.dataService.getData(key);
From that point I've got a different component which is listening for the retrieval of the data and will then make a graph.
The problem I'm facing is the data service is successfully making the API call and getting back a JSON response but I cannot figure out how to pass that data/object over to the time-graph.
private segResponse$: Observable<SegmentResponse>;
@Output() dataRetrieved: EventEmitter<Observable<SegmentResponse>> = new EventEmitter();
...
getData(key: string) {
this.segResponse$ = this.httpClient.get<SegmentResponse>(this.baseUrl + key);
console.log('seg resp in service ' + this.segResponse$);
this.dataRetrieved.next(this.segResponse$);
}
I can see the console response just fine here but in my time-graph, the object (of type SegmentResponse) is undefined. "state" is just one of the properties (string) of SegmentResponse.
ngOnInit() {
this.dataService.dataRetrieved.subscribe( data => {
this.segResponse = data;
console.log('time graph got data ' + this.segResponse.state);
});
}
So why is it undefined in the first place? What do I need to do to get the SegmentReponse from the data service successfully over to time-graph?
I'm guessing the .next part is not waiting for the successful return of the httpClinet.get but like I said, I'm at the edge of my knowledge of Angular + search-fu ability to find the right words go look up to get a solution.
add a comment |
I'm working on a project for school and I've hit a wall with my inexperience with Angular. I've got a left-nav component with a bunch of checkbox selections, and when a user selects one of the "codes" an API call goes off to our data store to retrieve all the values for a given "key"
this.dataService.getData(key);
From that point I've got a different component which is listening for the retrieval of the data and will then make a graph.
The problem I'm facing is the data service is successfully making the API call and getting back a JSON response but I cannot figure out how to pass that data/object over to the time-graph.
private segResponse$: Observable<SegmentResponse>;
@Output() dataRetrieved: EventEmitter<Observable<SegmentResponse>> = new EventEmitter();
...
getData(key: string) {
this.segResponse$ = this.httpClient.get<SegmentResponse>(this.baseUrl + key);
console.log('seg resp in service ' + this.segResponse$);
this.dataRetrieved.next(this.segResponse$);
}
I can see the console response just fine here but in my time-graph, the object (of type SegmentResponse) is undefined. "state" is just one of the properties (string) of SegmentResponse.
ngOnInit() {
this.dataService.dataRetrieved.subscribe( data => {
this.segResponse = data;
console.log('time graph got data ' + this.segResponse.state);
});
}
So why is it undefined in the first place? What do I need to do to get the SegmentReponse from the data service successfully over to time-graph?
I'm guessing the .next part is not waiting for the successful return of the httpClinet.get but like I said, I'm at the edge of my knowledge of Angular + search-fu ability to find the right words go look up to get a solution.
add a comment |
I'm working on a project for school and I've hit a wall with my inexperience with Angular. I've got a left-nav component with a bunch of checkbox selections, and when a user selects one of the "codes" an API call goes off to our data store to retrieve all the values for a given "key"
this.dataService.getData(key);
From that point I've got a different component which is listening for the retrieval of the data and will then make a graph.
The problem I'm facing is the data service is successfully making the API call and getting back a JSON response but I cannot figure out how to pass that data/object over to the time-graph.
private segResponse$: Observable<SegmentResponse>;
@Output() dataRetrieved: EventEmitter<Observable<SegmentResponse>> = new EventEmitter();
...
getData(key: string) {
this.segResponse$ = this.httpClient.get<SegmentResponse>(this.baseUrl + key);
console.log('seg resp in service ' + this.segResponse$);
this.dataRetrieved.next(this.segResponse$);
}
I can see the console response just fine here but in my time-graph, the object (of type SegmentResponse) is undefined. "state" is just one of the properties (string) of SegmentResponse.
ngOnInit() {
this.dataService.dataRetrieved.subscribe( data => {
this.segResponse = data;
console.log('time graph got data ' + this.segResponse.state);
});
}
So why is it undefined in the first place? What do I need to do to get the SegmentReponse from the data service successfully over to time-graph?
I'm guessing the .next part is not waiting for the successful return of the httpClinet.get but like I said, I'm at the edge of my knowledge of Angular + search-fu ability to find the right words go look up to get a solution.
I'm working on a project for school and I've hit a wall with my inexperience with Angular. I've got a left-nav component with a bunch of checkbox selections, and when a user selects one of the "codes" an API call goes off to our data store to retrieve all the values for a given "key"
this.dataService.getData(key);
From that point I've got a different component which is listening for the retrieval of the data and will then make a graph.
The problem I'm facing is the data service is successfully making the API call and getting back a JSON response but I cannot figure out how to pass that data/object over to the time-graph.
private segResponse$: Observable<SegmentResponse>;
@Output() dataRetrieved: EventEmitter<Observable<SegmentResponse>> = new EventEmitter();
...
getData(key: string) {
this.segResponse$ = this.httpClient.get<SegmentResponse>(this.baseUrl + key);
console.log('seg resp in service ' + this.segResponse$);
this.dataRetrieved.next(this.segResponse$);
}
I can see the console response just fine here but in my time-graph, the object (of type SegmentResponse) is undefined. "state" is just one of the properties (string) of SegmentResponse.
ngOnInit() {
this.dataService.dataRetrieved.subscribe( data => {
this.segResponse = data;
console.log('time graph got data ' + this.segResponse.state);
});
}
So why is it undefined in the first place? What do I need to do to get the SegmentReponse from the data service successfully over to time-graph?
I'm guessing the .next part is not waiting for the successful return of the httpClinet.get but like I said, I'm at the edge of my knowledge of Angular + search-fu ability to find the right words go look up to get a solution.
asked Nov 25 '18 at 20:40
Nick MaxwellNick Maxwell
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
httpClient.get returns an Observable. You need to subscribe to it.
getData(key: string) {
this.segResponse$ = this.httpClient.get<SegmentResponse>(this.baseUrl + key).subscribe((response) => {
console.log('seg resp in service ' + response);
this.dataRetrieved.next(response);
});
}
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%2f53471737%2fangular-eventemitter-returning-undefined-object%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
httpClient.get returns an Observable. You need to subscribe to it.
getData(key: string) {
this.segResponse$ = this.httpClient.get<SegmentResponse>(this.baseUrl + key).subscribe((response) => {
console.log('seg resp in service ' + response);
this.dataRetrieved.next(response);
});
}
add a comment |
httpClient.get returns an Observable. You need to subscribe to it.
getData(key: string) {
this.segResponse$ = this.httpClient.get<SegmentResponse>(this.baseUrl + key).subscribe((response) => {
console.log('seg resp in service ' + response);
this.dataRetrieved.next(response);
});
}
add a comment |
httpClient.get returns an Observable. You need to subscribe to it.
getData(key: string) {
this.segResponse$ = this.httpClient.get<SegmentResponse>(this.baseUrl + key).subscribe((response) => {
console.log('seg resp in service ' + response);
this.dataRetrieved.next(response);
});
}
httpClient.get returns an Observable. You need to subscribe to it.
getData(key: string) {
this.segResponse$ = this.httpClient.get<SegmentResponse>(this.baseUrl + key).subscribe((response) => {
console.log('seg resp in service ' + response);
this.dataRetrieved.next(response);
});
}
answered Nov 25 '18 at 21:04
finicofinico
671612
671612
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.
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%2f53471737%2fangular-eventemitter-returning-undefined-object%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