4

The browser console displays the following error:

zone.js:668 Error: Uncaught (in promise): [object Undefined]
    at resolvePromise (zone.js:814)
    at resolvePromise (zone.js:771)
    at zone.js:873
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
    at drainMicroTaskQueue (zone.js:595)
    at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:500)
    at invokeTask (zone.js:1540)
    at globalZoneAwareCallback (zone.js:1577)

I have no idea where to look in the source code.

UPDATE: The same page opened under Firefox gives the message:

Error: Uncaught (in promise): [object Undefined]
Stack trace:
resolvePromise@http://localhost:4200/polyfills.js:3131:31
makeResolver/<@http://localhost:4200/polyfills.js:3041:17
setError@http://localhost:4200/scripts.js:979:21
messageCallback@http://localhost:4200/scripts.js:1092:25
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invokeTask@http://localhost:4200/polyfills.js:2738:17
./node_modules/zone.js/dist/zone.js/</Zone.prototype.runTask@http://localhost:4200/polyfills.js:2505:28
./node_modules/zone.js/dist/zone.js/</ZoneTask.invokeTask@http://localhost:4200/polyfills.js:2813:24
invokeTask@http://localhost:4200/polyfills.js:3857:9
globalZoneAwareCallback@http://localhost:4200/polyfills.js:3883:17
 zone.js:668

UPDATE: In case the error is related to my newly added interceptor source code, here is how I handle a possible exception:

  intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    return this.addAuthHeader(request, next);
  }

  private addAuthHeader(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    console.log('=======>> Intercepting the http request to add the jwt token in the header');
    const tokenPromise: Promise<any> = this.keycloakClientService.getToken();
    const tokenObservable: Observable<any> = Observable.fromPromise(tokenPromise);
    tokenObservable.map(authToken => {
      console.log('Token value: ' + authToken);
      // Clone the request before it is sent to the server
      // as the original request is immutable and cannot be changed
      request = request.clone({
        setHeaders: {
          'Authorization': AUTH_HEADER_PREFIX + authToken
        }
      });
      console.log('=======>> The request has been cloned');
    });

    console.log('Handle the request in the interceptor chain');
    return next.handle(request)
      .catch(response => {
        if (response instanceof HttpErrorResponse) {
           if (response.status === 401) {
             // TODO redirect to the login route or show a modal
           }
          console.log('The error has been handled by the interceptor', response);
        }

        return Observable.throw(response);
      })
      .do((response: HttpEvent<any>) => {
        if (response instanceof HttpResponse) {
          console.log('The response has been handled by the interceptor', response);
        }
      });
  }
Stephane
  • 11,836
  • 25
  • 112
  • 175
  • I ain't sure could you give it a try `console.log((new Error('throw')).stack);` in your root module `constructor` – Vikas May 20 '18 at 14:47
  • [generic comment] when i get error messages that don't give me a clue what's going on, usually first thing i try is doing `ng serve --prod`. More often than not that gives me build errors that are in some way related to runtime errors. – dee zg May 20 '18 at 15:00
  • @Vikas I coded the constructor as `AppModule() { console.log((new Error('throw')).stack); }` but it outputs the very same error. – Stephane May 20 '18 at 15:21
  • @deezg I served it with the production flag and it did not really start, this time not showing the login form, and the error displayed was then `main.2cd4920….js:1 Uncaught Error: No NgModule metadata found for 'e'` – Stephane May 20 '18 at 15:26
  • did it give you build errors or only runtime errors? – dee zg May 20 '18 at 15:28
  • @deezg It's a runtime error. The build is fine. – Stephane May 20 '18 at 16:20
  • yeah, that's a bummer. have you done any upgrades before it stopped working? angular, angular cli, rxjs...? – dee zg May 20 '18 at 16:23
  • @deezg No, this error comes along some new code that I put in place, an interceptor on a JWT token using the `@auth0/angular-jwt` dependency found at `https://github.com/auth0/angular2-jwt`. And I wonder if that promise error could come, not from my own source code, but from the one of a dependency... how to know ? – Stephane May 20 '18 at 17:18
  • 4
    Please don't close this question. While I agree it might appear to be off topic. This specific error is not a random error like it might appear. There are specific reasons as to why it happens, and we need this documented on the internet so that people will find solutions. As I've run into this error and you are basically left hopeless trying to resolve it. Until the Angular and WebPack team fix their code so that these errors are better. There isn't really much we can do to make this "on topic". – Reactgular May 20 '18 at 19:09
  • 1
    oh oh, wait...you mentioned this happening after you've added http interceptor. are you maybe not wrapping your error case in the interceptor into `Observable.throw/throwError` but just leaving it as pure `Error'? You must return Observable emitting error, not error itself. ref: https://angular.io/guide/http#getting-error-details – dee zg May 21 '18 at 04:53
  • I forgot to say, the complete project is at `https://github.com/stephaneeybert/ng-hero` if that can be of interest to any. – Stephane May 27 '18 at 13:05

2 Answers2

9
zone.js:668 Error: Uncaught (in promise): [object Undefined]

This is an error that happens outside the scope of your source code. That's why there is no stack trace that relates to something you can debug directly. You might try changing your source code or adding console log messages, but my guess is that it won't help you find it.

It's most likely a configuration mistake.

You can get this error when Angular fails to load modules. Check each of your modules and make sure all of the declarations, imports, exports, etc.. etc.. are correct. Make sure that you use proper injection tokens and injectable references.

If this happens after the application starts. It might be a failure to lazy load a module. Again, repeat the above steps.

If that doesn't help find the cause, then you need to remove as many things as possible and slowly add them back. Until you find the culprit.

Create an empty component that does nothing and make this your entry component. Comment out all of your providers and comment out all of your declarations. Keep commenting out things the main module uses until the application starts successfully.

Slowly, add back your providers and declarations until it breaks again.

This should help you narrow down where in your module configuration the problem is. Hopefully, this is what the cause of the problem is.

There are of course many other things that could trigger this error.

  • Try wrapping source code that uses third-party (none-Angular) libraries in a zone.runOutsideAngular(()=>{....}) so that any unhandled errors are not tracked by zones.
  • Build your application in production mode, but enable source maps for debugging. ng build --prod --sourcemaps
  • Add a ErrorHandler service and see if it catches the error. As this will let you inspect the thrown object. You add a provider to your module like this {provide: ErrorHandler, useClass: MyHandlerService} where MyHandlerService implements ErrorHandler.
Reactgular
  • 52,335
  • 19
  • 158
  • 208
  • I tried the production build and added an error handler but it didn't catch the error. Only my own dummy test error was caught. – Stephane May 27 '18 at 13:07
  • I pinpointed the error and described it at https://stackoverflow.com/questions/50005495/swallowed-message-error-uncaught-in-promise-object-undefined – Stephane Jun 07 '18 at 12:55
  • 1
    The ` --sourcemaps` option seems to be gone in Angular 6. Here is my mileage: `ng build --prod --sourcemaps Unknown option: '--sourcemaps'` – Stephane Jun 20 '18 at 08:46
  • i just added catch and solved thanks return await this.loadingCtrl.dismiss().then(() => console.log('dismissed')).catch(() => {}); – saber tabatabaee yazdi Oct 08 '19 at 05:09
2
this.modalService.open(content, {
  ariaLabelledBy: "title",
  size: "lg",
  backdrop: "static"
}).result.then((acao) => {

}, () => { // Add

});
Alessandro
  • 305
  • 4
  • 12
  • Would you mind editing your answer to include additional information about how this addresses the problem? – Jeremy Caney Dec 06 '20 at 00:13
  • Quite coincidence that this code helps on my debugging. Though i not sure how/why it works – Jmeei Jan 13 '23 at 04:51
  • 1
    Can refer to this issue to justify the solution: https://github.com/shlomiassaf/ngx-modialog/issues/188#issuecomment-247261657 – Jmeei Jan 13 '23 at 05:10