1

I am having the same challenge as was faces in the SO Post here I am getting undefined in the subscribe method in my component.ts, even though in my service i have the data. See codes below p.component.ts

 private getPayItems():void{
    console.log('In getPayItems');
    this._payItemService.getPayItems()
    .subscribe(data => { 
        this.payItemArray = data;
        console.log(data);
    },
    (error:any) =>{
         this.alerts.push({ msg: error, type: 'danger', closable: true }); 
    }) 
}

p.service.ts

getPayItems():Observable<Payitem[]>{

    let  actionUrl = this.url +  "/GetPayItem";

    return this._http.get(actionUrl, { headers: this.headers })
        .map((response: Response) => { 
            <Payitem[]>response.json() ;
             console.log(<Payitem[]>response.json()); //This logs the Object
        })
        .catch(this.handleError);
}

1 Answer 1

5

As you used {} it needs explicit return from function. So you have to return <Payitem[]>response.json() from map function.

getPayItems():Observable<Payitem[]>{

    let  actionUrl = this.url +  "/GetPayItem";

    return this._http.get(actionUrl, { headers: this.headers })
        .map((response: Response) => { 
             console.log(<Payitem[]>response.json()); //This logs the Object
            return <Payitem[]>response.json() ;
        })
        .catch(this.handleError);
}

Otherwise below would be shorthand syntax

getPayItems():Observable<Payitem[]>{
    let  actionUrl = `${this.url}/GetPayItem`;
    return this._http.get(actionUrl, { headers: this.headers })
        .map((response: Response) => <Payitem[]>response.json())
        .catch(this.handleError);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.