I'm doing the nativescript/angular tutorial and I found something in the code that I don't understand and want some clarification.
In chapter 4 (Nativescript modules) when they do a http.get resquest to retrieve the Grocery List and they get the Observable I notice that it is passed throught some maps operator, here is the code:
import { Injectable } from "@angular/core";
import { Http, Headers } from "@angular/http";
import { Observable } from "rxjs/Rx";
import "rxjs/add/operator/map";
import { Config } from "../config";
import { Grocery } from "./grocery";
@Injectable()
export class GroceryListService {
constructor(private http: Http) {}
load() {
let headers = new Headers();
headers.append("Authorization", "Bearer " + Config.token);
return this.http.get(Config.apiUrl + "Groceries", {
headers: headers
})
.map(res => res.json())
.map(data => {
let groceryList = [];
data.Result.forEach((grocery) => { //<------HERE
groceryList.push(new Grocery(grocery.Id, grocery.Name));
});
return groceryList;
})
.catch(this.handleErrors);
}
handleErrors(error: Response) {
console.log(JSON.stringify(error.json()));
return Observable.throw(error);
}
}
My question is, what does "Result" means in the second .map
Why they don't simply put
data.forEach((grocery) => {
I ask because I'm not sure if it is an object property of the resulting observable from .map(res => res.json) or something else.
Could you point me to some documentation of where does that "Result" come from and what it means?
First of all, this line .map(res => res.json())
parses the response body into a json object. Then the second map
allows access to this json object under data
argument. The json object represented by data
is actually a wrapper around the actual response result data using the Result
as the key mapped to the data returned from the backend which follows this security advise HERE. So data.Result
is just a key mapped to the actual data returned from the backend. The backend could have used a different name for the key, e.g. secret, then you would do data.secret
to obtain the data returned from the server