typescript - subscribe is not a function error thrown when trying to use JSON file in Angular 2 -
i'm trying import json file service , receiving error
error: uncaught (in promise): typeerror: this._qs.getbasicinfo(...).subscribe not function(…)
as of right service file looks
@injectable() export class questionservice { basicinfodata: observable<any>; constructor(private http: http){} getbasicinfo() { this.basicinfodata = this.http.get('app/questionnaire/questions.json').map(res => res.json()).subscribe(data => this.basicinfodata = data ) observable<any> return this.basicinfodata; } } and component looks this
@component({ moduleid : module.id, selector : 'questionnaire-page', templateurl : './questionnaire.component.html', providers : [ questionservice ] }) export class questionnairepage implements oninit, onchanges { basicinfo: observable<any>; constructor(private _qs: questionservice, private fbuild: formbuilder) {} ngoninit() { this._qs.getbasicinfo().subscribe( data => this.basicinfo = data ); console.log(this.basicinfo); } } prior trying call data service had working fine calling directly ngoninit() of component this
this.http.get('app/questionnaire/questions.json').subscribe(res => { this.questions = res.json() console.log(this.questions); }); i started out trying use same thing , read, added observable. came across post rxjs saying problem subscribing directly data before got there , fix use .map() first subscribe that. changed error undefined error 1 i'm getting figured step in right direction. i've been , forth trying sorts of things , either creates error indicating breaks before point or same error same snip of code. why happening?
this code returns subscription
getbasicinfo() { this.basicinfodata = this.http.get('app/questionnaire/questions.json').map(res => res.json()).subscribe(data => this.basicinfo = data ) observable<any> return this.basicinfodata; } } subscribe method of observable not of subscription. want probably
getbasicinfo() { return this.http.get('app/questionnaire/questions.json') .map(res => res.json()); } this way observable returned can subscribed to.
Comments
Post a Comment