Unhandled Promise Rejection AOT(Ahead Of Time) Angular 2

2.2k views Asked by At

I have an small Angular 2 app that fetches the data from ASP.net MVC Web API. Using Authentication. I am trying to use Ahead Of Time compiler in Angular 2. I am running the following command

node_modules \ .bin\ngc -p src

but i am getting following error

(node:13640) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Right-hand side of 'instanceof' is not an object (node:13640) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

can some one tell me how to resolve it

This is my directory structure

I have move the tsconfig.json from root to src folder as it was directed in this link

Directory structure

That's how i am using promise

I have made services from which i return the promise like this:

public get(servicename: string) {
    this.colorlog.log('get ' + servicename + " ",enmMessageType.Info);
    return this.http.get(this.ngWEBAPISettings.apiServiceBaseUri + "api/" + servicename + "/", this.ngWEBAPISettings.getConfig()).toPromise();
}

and consuming it in component like this

get() {
    var promise = this.setupServicePromise.get(this.SERVICE_NAME);

    promise.then((response: any) => {
      this.colorlog.log('in then promise ProductComponent get' + response,   enmMessageType.Data);
      this.vm.Products = response.json();
    }).catch((error: any) => {
      this.colorlog.log("Error Occurred in product", enmMessageType.Error);
    });
}

this is my ts.config file.

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es2015", "dom"],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}
1

There are 1 answers

1
rashfmnb On BEST ANSWER

I have accomplish this in following steps

First

I have updated the version as mentioned by MrJSingh

After updateh package.json dependencies section become like this.

"dependencies": {
  "@angular/common": "~2.4.0",
  "@angular/compiler": "~2.4.0",
  "@angular/compiler-cli": "^2.4.1",
  "@angular/core": "~2.4.0",
  "@angular/forms": "~2.4.0",
  "@angular/http": "~2.4.0",
  "@angular/platform-browser": "~2.4.0",
  "@angular/platform-browser-dynamic": "~2.4.0",
  "@angular/router": "~3.4.0",
  "core-js": "^2.4.1",
  "rxjs": "5.0.1",
  "zone.js": "^0.7.4"
},

Second

After that i have made changes to app.module.ts file and change useValue to useFactory

change this

let authService = new AuthService(new LocalStorage());
providers: [
  {provide:AuthService,useValue:authService},
  AdminAuthGuard,
  UserAuthGuard,
  SetupServicePromise,
  SetupServiceObservables
]

to this

export function authServiceFactory() {
  return new AuthService(new LocalStoragee());
}

providers: [
  {provide:AuthService,useFactory:authServiceFactory},
  AdminAuthGuard,
  UserAuthGuard,
  WebAPISettings,
  LoginService,
  SetupServicePromise,
  SetupServiceObservables
]

Finally command runs successfully after these two steps.

Note

For Latest version use declaracions instead of providers

declarations: [
  {provide:AuthService,useFactory:authServiceFactory},
  AdminAuthGuard,
  UserAuthGuard,
  WebAPISettings,
  LoginService,
  SetupServicePromise,
  SetupServiceObservables
]

For more detail check this link