angular2-jwt ERROR TypeError: Observable_1.Observable.defer is not a function at AuthHttp.request (angular2-jwt.js:140)
See original GitHub issueI am looking to reuse a service developed for an Ionic project in another project. Both applications are based on the same versions of libraries.
The service (Provider) has the following code:
import
{Injectable} from ‘@angular/core’;
import
{AuthHttp} from ‘angular2-jwt’;
import
{ Headers, RequestOptions } from ‘@angular/http’;
import
‘rxjs/add/operator/map’;
import
‘rxjs/add/operator/toPromise’;
import
* as AppConfig from ‘…/app/config’;
import
{Storage} from ‘@ionic/storage’;
@Injectable()
export
class BusinessService {
private
cfg: any;
constructor(
private
authHttp: AuthHttp,
public
storage: Storage
`)`
`{`
`this.cfg` = AppConfig.cfg;
`}`
listItems()
{
`return this.authHttp.get(this.cfg.apiUrl + this.cfg.businessServices.listItems())
.toPromise()
.then(rs => {
console.log(rs, rs.json());
return rs.json();
});
}
I call the method in the home page like this: import {BusinessService} from ‘…/…/services/business-service’; … this.BusinessService.listItems() .then(data => {
...
})
.catch(e => console.log(" error", e));
I always get the following error:
core.es5.js:1020 ERROR TypeError: Observable_1.Observable.defer is not a function at AuthHttp.request (angular2-jwt.js:140) at AuthHttp.requestHelper (angular2-jwt.js:104) at AuthHttp.get (angular2-jwt.js:151) at BusinessService.webpackJsonp.355.BusinessService.listItems (main.js:398) at HomePage.webpackJsonp.354.HomePage.listItems(orders.ts:20)
In the app.module file, I made the following settings: import {HttpClientModule, HttpClient} from ‘@angular/common/http’; import {HttpModule, Http} from ‘@angular/http’; import {AuthHttp, AuthConfig,JwtHelper} from ‘angular2-jwt’;
… imports: [ BrowserModule, HttpClientModule, HttpModule, … providers: [ StatusBar, SplashScreen, JwtHelper, { provide: AuthHttp, useFactory: getAuthHttp, deps: [Http] }, BusinessService
Here is the result of my ionic info: ionic (Ionic CLI) : 4.0.5 (C:\Users\kamahunda\AppData\Roaming\npm\node_modules\ionic) Ionic Framework : ionic-angular 3.8.0 @ionic/app-scripts : 3.0.1
Cordova:
cordova (Cordova CLI) : 8.0.0 Cordova Platforms : android 7.0.0
System:
NodeJS : v8.9.3 (C:\Program Files\nodejs\node.exe) npm : 6.4.1 OS : Windows 10
Environment:
ANDROID_HOME : not set
Here are the library versions in my package.json “angular2-jwt”: “^0.2.3”, “rxjs”: “^6.2.0”, “rxjs-compat”: “^6.2.0”, “@angular/common”: “4.4.4”, “@angular/compiler”: “4.4.4”, “@angular/compiler-cli”: “4.4.4”, “@angular/core”: “4.4.4”, “@angular/forms”: “4.4.4”, “@angular/http”: “4.4.4”, “@angular/platform-browser”: “4.4.4”,
Any help would be welcome.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:5
você terá que ir no arquivo angular2-jwt.js ! no começo do código alterar e deixando essa duas linha aqui
var Observable_1 = require(“rxjs”); require(“rxjs/operator”);
No codigo da linha 138 ate 146 um retorno lá você vai ver apaque o Observable e deixe assim Observable_1.defer só isso para essa versão
return Observable_1.defer(function () { var token = _this.config.tokenGetter(); if (token instanceof Promise) { return Observable_1.Observable.fromPromise(token).mergeMap(function (jwtToken) { return _this.requestWithToken(req, jwtToken); }); } else { return _this.requestWithToken(req, token); } });
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇♂️