Error when calling method in Provider


#1

I have the following component

newsfeed.ts

import { Component, Inject } from ‘@angular/core’;
import { NavController, Tabs } from ‘ionic-angular’;
import { AuthService } from ‘…/…/services/auth/auth.service’;
import { ReportService } from ‘…/…/services/reports/report.service’;
import { EnvVariables } from ‘…/…/app/environment-variables/environment-variables.token’;

@Component({
selector: ‘page-newsfeed’,
templateUrl: ‘newsfeed.html’
})

export class Newsfeed {
report_list: {};
apiUrl: any;

static get parameters()
{
return [[Tabs]];
}

constructor(
public tab: Tabs,
public navCtrl: NavController,
public authService: AuthService,
public reportService: ReportService,
@Inject(EnvVariables) public envVariables) {

this.tab = tab;

}

ionViewDidLoad() {
this.reportList();
}
openReportTab() {
this.tab.select(2);
}

reportList() {
this.reportService.get_report_list().subscribe((results) => {
this.report_list = results;
})

}

}

and the following service

report.service.ts

import { AuthHttp } from ‘angular2-jwt’;
import { Inject, Injectable } from ‘@angular/core’;
import { Storage } from ‘@ionic/storage’;
import { Http, Headers, Response, RequestOptions } from ‘@angular/http’;
import { Observable } from “rxjs/Observable”;
import { EnvVariables } from ‘…/…/app/environment-variables/environment-variables.token’;
import { GlobalErrorHandler } from ‘…/providers/error-handler’;
import { ToastHandler } from ‘…/providers/toasts’;

@Injectable()

export class ReportService {

constructor(public authHttp: AuthHttp, private http: Http, @Inject(EnvVariables) public envVariables){

    var headers = new Headers();
  headers.append("Accept", 'application/json');
  headers.append('Content-Type', 'application/json');
  var options = new RequestOptions({headers: headers});

}

public get_report_list(): Observable {
console.log(“get report list”)
var url = “/api/v1/newsfeed”;
return this.authHttp.get(this.envVariables.apiEndpoint + url)
.map(res => res.json().data)
}

}

In my code, I am calling the get_report_list method in the constructor of the newsfeed component but I keep getting this error which is completely absurd

Unhandled Promise rejection: Cannot read property ‘get_report_list’ of undefined ; Zone:
angular ; Task: Promise.then ; Value: [object Object] TypeError: Cannot read property ‘get_report_list’ of
undefined at Newsfeed.webpackJsonp.304.Newsfeed.reportList (http://localhost:8100/build/main.js:387:28) at
Newsfeed.webpackJsonp.304.Newsfeed.ionViewDidLoad (http://localhost:8100/build/main.js:362:14) at
ViewController._lifecycle (http://localhost:8100/build/vendor.js:18359:33) at ViewController._didLoad
(http://localhost:8100/build/vendor.js:18232:14) at Tab.NavControllerBase._didLoad
(http://localhost:8100/build/vendor.js:47611:18) at t.invoke
(http://localhost:8100/build/polyfills.js:3:8488) at Object.onInvoke
(http://localhost:8100/build/vendor.js:4796:37) at t.invoke
(http://localhost:8100/build/polyfills.js:3:8428) at r.run (http://localhost:8100/build/polyfills.js:3:3686)
at NgZone.run (http://localhost:8100/build/vendor.js:4665:62)

Completely confused and i dont know why this error exists. Can someone point me in the right direction?

Thanks


#2

If you think that’s what is causing the problem, why didn’t you post that line?

Also, you don’t need @Inject. I doubt that’s causing your problem, but it is more clutter than you need. Just public variable: Type.


#3

How old is this code?

Wasn’t static get parameters() Ionic 2 beta syntax?


#4

Yes. Now deprecated, though I’ve never tested to see what happens if you still use it.


#5

I did post it. im calling it via the reportList() in newsfeed component


#6

Your constructor is basically empty. Your English does not refer to the version of code you posted. That’s a danger sign for anyone trying to help debug on a forum, just FYI. So you might want to decide what you want to communicate here.


#7

oops sorry. it was in my ionViewLoad() but anywys i removed the static get parameters method and it seems to be working so thanks @AaronSterling @SigmundFroyd for at least pointing me in the right direction.