Skip to content

Instantly share code, notes, and snippets.

@sharikovvladislav
Created August 29, 2017 11:36
Show Gist options
  • Save sharikovvladislav/041da37b87c1f74976799c548065fcf2 to your computer and use it in GitHub Desktop.
Save sharikovvladislav/041da37b87c1f74976799c548065fcf2 to your computer and use it in GitHub Desktop.
private getGroups(): Observable<any> {
// ....
return this.http.get(_url, options)
.map(response => {
var responseAsObject = response.json();
console.log(responseAsObject);
return responseAsObject;
})'
}
@itanimo
Copy link

itanimo commented Aug 29, 2017

http://i.imgur.com/a9kyM4Q.jpg

two proper responses. Still can't map it to a public variable.

@sharikovvladislav
Copy link
Author

Cool! Next one. Usage.

constructor() {
  this.getGroups()
    .subscribe(roles => {
      console.log(roles); // <--- ?
      this.roles = roles;
    });
  }
}

@sharikovvladislav
Copy link
Author

?

@itanimo
Copy link

itanimo commented Aug 29, 2017

undefined

@sharikovvladislav
Copy link
Author

Show me your current getGroups implementation.

@itanimo
Copy link

itanimo commented Aug 29, 2017

private getGroups() : Observable {
console.log("In Groups");
let _url = "http://url.....";
let headers = new Headers();
headers.append('X-User', sessionStorage.getItem('username'));
headers.append('X-Token', sessionStorage.getItem('token'));
headers.append('X-AccessTime', sessionStorage.getItem('AccessTime'));
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({ headers: headers });

return this.http.get(_url, options)
.map(response => {
  var responseAsObject = response.json();
  this.roles = responseAsObject;
})

}

@sharikovvladislav
Copy link
Author

so how do you expect this work?

My code: https://gist.github.com/sharikovvladislav/041da37b87c1f74976799c548065fcf2#file-lulz-L7. Note return statement.

Your code:

return this.http.get(_url, options)
.map(response => {
  var responseAsObject = response.json();
  this.roles = responseAsObject;
})

You return undefined. Thats why you get undefined in the usage.

@sharikovvladislav
Copy link
Author

Did that solved your problem?

@itanimo
Copy link

itanimo commented Aug 29, 2017

Sort of, even if I return the variable I can't map it to my array. But you helped clarify part of the problem so I have that to thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment