I am trying to utilize the Promise operator
Firstly, I imported rxjs:
import 'rxjs/add/operator/toPromise';
then in my component, I implemented it
ngOnInit() {
this.EJnames= this.dataservice.getResults();
this.generalinfosservice.getResults("nothing").then(data=>{this.data=data; console.log(data)}); //Log 1
console.log(this.data); // log 2
}
An error occurred stating:
ERROR TypeError: this.generalinfosservice.getResults(...).toPromise is not a function
I have double-checked my Package.json, and confirmed that I have rxjs:
"rxjs": "^5.1.0",
Here is the generalinfosservice.getresults
function
import { Injectable } from '@angular/core';
import { RmpmService} from '../../shared/abstract-classes/service'
import { HttpClient } from '@angular/common/http';
@Injectable()
export class GeneralinfosService extends RmpmService {
constructor(public httpClient: HttpClient) { }
public getResults(body) {
let url;
let mockUrl
url = '/obtenirNomenclatures/typeNomenclatures/';
mockUrl = 'assets/data/group.json';
return this.post(url, mockUrl, body);
}
}
and post function which is within an abstract class :
post( url, mockUrl, body) {
if( environment.mocked ) {
return this.httpClient.get( mockUrl ).toPromise().then( data => { return data} );
} else {
return this.httpClient.post( url, JSON.stringify( body ) )
.toPromise()
.then( data => {
return data;
}, ( err: any ) => {
return this.handleError( err.message );
} );
}
}
PS: I am operating in a mocked environment