typescript - Angular 2: Observable not returning the object (nested) from service -


i have angular service returns user object. user object has own attributes plus array of walls. service returns observable calling component. in service, able create user object json returned http service. however, when subscribe service in component, object returned null. doing wrong here?

// user.ts  import { wall } './wall';  export class user {     id: number;     entrytype: number;     usertype: number;     subscriptiontype: number;     iscoach: boolean;     username: string;     email: string;     name: string;     password: string;     created: string;     memberwalls: wall[]; } 
//wall.ts export class wall {     title: string;     viewitem_id: number; } 
//authentication.service.ts  authenticate(authrequest: login): observable<user> {         let url: string = this.apiurl + appsettings.login_service;         let headers = new headers({             'content-type': appsettings.content_type_header, //'; charset=utf-8',             'client-secret': appsettings.client_secret,             'client-id': appsettings.client_id         });          let options = new requestoptions({ headers: headers });          return this._http.post(url, authrequest, options) //              .map(data => {                 this.authenticated(data);             })             .catch(this.handleerror);      }      private authenticated(res: response) {         let body = res.json();         if (body.statuscode === 200) {             localstorage.setitem('auth_token', res.headers.get("access-token"));             let user1: user = body.data;             //the user object fine here.              //that means json , user class structure match             console.log(user1);             return body.data || {};         }         else {             return {};         }     } 
//login.component.ts  login() {         this.errormessage = '';         this.currentuser = null;              this._authservice.authenticate(this.loginmodel)                 .subscribe(user1 => this.currentuser = user1,                 error => this.handleerror( error));    //the user1 returned service null.     } 

you missing return statement when mapping result of post request.

in es6 , ts:

  • when arrow function defined using brackets, return statement mandatory.
  • when arrow function defined without brackets, es6 autogenerate return statement returning value of expression provided after arrow sign

ex:

let f = (data) => this.authenticated(data);; // or  let f = (data) => { return this.authenticated(data); }; 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -