I mean, I know how to handle that data from the html view, but what I’m unsure is of how I can transform that data from the js and transform it. I think it is a very basic thing, but I tried without success.
Are you getting the data ok?
http.get return an observable so i thought you just couldnt use that directly as the respose i think.
this.http.get(url).map(res => res.json()).subscribe(res => {
// Do somethinng with your res
// Remeber that locations is an array so you have to access it as such
// res.locations[0].id
});
Thank you very much for all your answers. And thanks for the example, @aaronksaunders.
@sveinla, what I meant is that I don’t know is what’s the best approach to, for example, select and store only the locations. I was sure that it was something really easy, but I didn’t know how to do it. It is explained in @aaronksaunders’s example.
Again, thank you very much to all of you for your answers.
Please look at my console.log at my user_service.js.
I can console.log email or objectId but fail for user-token.
My problem is how to get the user-token and save it to localstorage so i can use it for another purpose??
my user_service.js
import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Headers, RequestOptions} from 'angular2/http';
import {Observable} from 'rxjs/Observable';
import {BackendService} from './backend_service';
import {Storage, LocalStorage, Events} from 'ionic-angular';
import 'rxjs/Rx';
@Injectable()
export class UserService {
static get parameters() {
return [[Http], [BackendService], [Events]];
}
constructor(http, backendService, events) {
this.http = http;
this.backendService = backendService;
this.events = events;
this.user = {};
this.storage = new Storage(LocalStorage);
this.HAS_LOGGED_IN = 'hasLoggedIn';
this.Backend_Login_Url = backendService.BackendLoginUrl
this.Backend_Register_Url = backendService.BackendRegisterUrl
this.Backend_Logout_Url = backendService.BackendLogoutUrl
this.Backend_Header = backendService.BackendHeader
}
userLogin(userData)
{
let body = JSON.stringify({ login: userData.email, password: userData.password });
let headers = new Headers(this.Backend_Header);
let options = new RequestOptions({ headers: headers });
return this.http.post(this.Backend_Login_Url, body, options)
.map((res) => {
let data = res.json();
console.log('data.email at user_service', data.email); // result : personal@gmail.com
console.log('data.objectId at user_service', data.objectId); // result C8FE36A2-9396-13C5-FF9C-8F8EF7F07900
console.log('data.user-token at user_service', data.user-token); // result Reference error : Token is not defined
this.storage.set('userStorage', JSON.stringify(data)); // result : success
return data;
});
}