38

I'm having a problem with regards of implementing a resolver on my routes as it has no issue until I include InitialDataResolver on my routing module.

pages-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { FrontComponent } from '../layouts/front/front.component';
import { HomeComponent } from './home/home.component';
import { DocsComponent } from './docs/docs.component';
import { InitialDataResolver } from './../shared/resolvers/initial-data.resolver';

const routes: Routes = [
  {
    path: '',
    component: FrontComponent,
    children: [
      { path: '', component: HomeComponent },
      { path: 'docs', component: DocsComponent }
    ],
    resolve: {
      init: InitialDataResolver
    },
  }
];

@NgModule({
  imports: [ RouterModule.forChild(routes) ],
  exports: [ RouterModule ],
  providers: [ InitialDataResolver ]
})
export class PagesRoutingModule { }

initial-data.resolver.ts

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router';
import { Observer } from 'rxjs/Observer';
import { AppInitService } from '../services/app-init.service';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class InitialDataResolver implements Resolve<any> {

  constructor(private appInitService: AppInitService) {}

  resolve(route: ActivatedRouteSnapshot,
          state: RouterStateSnapshot): Observable<any> {
    return Observable.create((observer: Observer<any>) => {

      this.appInitService.init()
          .subscribe(data => {
            this.appInitService.preload();
            observer.next(data);
            observer.complete();
          });

    });
  }

}

The error that I'm encountering is ERROR Error: "[object Object]". see the snapshot below: object Object Error

Riyenz
  • 2,498
  • 2
  • 9
  • 23

8 Answers8

118

This lack of detailed error occurred when using Mozilla Firefox. so what you need to do is to switch over to Google Chrome to see the specific error.

UPDATED:

You can also Store the error as Global Variable

enter image description here

then you can type temp0.message to see the actual error message

enter image description here

enter image description here

Riyenz
  • 2,498
  • 2
  • 9
  • 23
  • 10
    This is ridiculous. Didn't expect that at all. Thank you for your answer. Solved my issue. – Eray T Dec 17 '18 at 15:17
  • 1
    Wow. Last thing I was expecting as well. Thank you. – secondbreakfast Feb 08 '19 at 18:41
  • 2
    @zero01alpha Your welcome mate :) I hope Mozilla fix this in the future – Riyenz Feb 19 '19 at 06:22
  • 4
    I hope Angular make their framework work better on Mozilla – simbro Apr 10 '19 at 12:35
  • 1
    Funny thing is I have the error only on Firefox, Chrome works fine. How to debug this? :head-on-wall: – Jonathan Muller Apr 17 '19 at 05:12
  • 1
    This should not be the accepted answer. Saying "use a different browser" absolves companies and authors of ensuring their code works properly and promotes a browser mono-culture. – LocalPCGuy Dec 06 '19 at 20:54
  • @LocalPCGuy I think for debugging an error which is not visible on a specific browser then It is logical to switch for just few seconds since as a web developer we switch browsers every now and then to check if the code is working on all browsers. in this case its only an error that is not visible on Mozilla so it should be fine. anyways I updated my answer. – Riyenz Dec 10 '19 at 03:17
  • While I do agree that as web developers, we should have different browsers installed, and it isn't that hard to check, I pretty strongly disagree that this is a non-issue. It shouldn't be on the end developers to switch browsers to see if there are different error messages depending on the browser. Firefox/Mozilla is still a main-stream browser and as such, it should be tested against. – LocalPCGuy Jan 03 '20 at 14:23
22

In console you can store errors like

ERROR Error: "[object Object]"

as global variable

enter image description here

and then get error message from object temp0.message

kaminarifox
  • 413
  • 1
  • 5
  • 14
4

I have found solution for detailed error working in firefox. Its based on defining custom error handler and inspecting error properties yourself. After these steps, the errors were displayed properly, no need to switch to chrome.

  1. Define custom error handler class:
import { ErrorHandler } from '@angular/core'

export class MyErrorHandler implements ErrorHandler {

    handleError(error: any) {
        // console.error(Object.getOwnPropertyNames(error))
        // Object.getOwnPropertyNames(error).forEach(p => console.error(error[p]))
        console.error(error.fileName, error.lineNumber, ':', error.columnNumber, '\n', error.message, error.rejection)
    }

}
  1. Then register it as provider in main component
@NgModule({
    declarations: []
    imports: []
    providers: [{provide: ErrorHandler, useClass: MyErrorHandler}], // <-- register MyErrorHandler
    bootstrap: [RootComponent]
})
export class AppModule { }
Meyhem
  • 91
  • 1
  • 9
0
resolve(route: ActivatedRouteSnapshot,
      state: RouterStateSnapshot): Observable<any> {
    return Observable.create((observer: Observer<any>) => {

      this.appInitService.init()
          .subscribe(data => {
            this.appInitService.preload();
            observer.next(data);
            observer.complete();
          });

    }).map(item => !!item)
}

The code above will work for you.

if your rxjs version is 6.x you will use .pipe(map(item => !!item))

xsong
  • 630
  • 1
  • 5
  • 18
0

This error comes due to wrong router declaration. Also exact error never going to come in Firefox. Check route file in chrome. #angularjs-route

amku91
  • 1,010
  • 11
  • 21
0

The error could hide details on firefox if you build without --prod and host in web server, try to build with --prod if you are hosting on a web server to get details about the error.

Mosta
  • 868
  • 10
  • 23
0

one-time solution : find function defaultErrorLogger in dist/vendor.bundle.js and add:

for (var _i = 1; _i < arguments.length; _i++) {
    values[_i - 1] = arguments[_i];
}
console.log(arguments);
console.error.apply(console, values); code here

then refresh page without recompiling

0

Mainly this error is found when using database object from '@angular/fire'

this can be fixed by using this.db.object('/' + ....).subscribe(...)

Dhanush
  • 112
  • 7