0

I have a weird problem on an Angular 4 application. My component subscribes on a BehaviorSubject of a Service. On its subscribe ( or map, doesnt change in the end ) I call data.json() which the first time it runs ( coming from the default value of BehaviorSubject, which is an empty array ) it fails as array doesnt have a .json function.

//service.ts
private _errors: BehaviorSubject<any> = new BehaviorSubject([]);
public readonly errors: Observable<any> = this._errors.asObservable();
//component.ts
this.errors.subscribe(
  (data) => {
    const errorJSON = data.json();
  }
);

In some pc's this error doesnt prevent the rest application to run as exprected ( actually, the error isn't even thrown ) . In others this error gets thrown and the rest application fails.

All the pc's have exactly the same environment, running same commands and angular-cli and local angular dependencies. That happens either with ng serve or ng build. After many attempts to locate the root cause of this error, we found out that it has to do something with the build process, as the produced files from the "working pc" can successfully run on the "not working pc", while the opposite ( produced files from the "non working pc" transfered on the "working pc" ) fails.

What could the root of this problem be? Should the default behaviour of this error be to be thrown or not? In any case, has anyone experienced something similar or have anything to propose as to why this happen?

  • It seems you want to hide the error instead of fixing it. Just don't call json() on an object that doesn't have any json() method. Don't use `any` as your subject and observable generic type. Use the actual type of the events that they emit. That way, the compiler will prevent you from doing incorrect things such as calling json() on an array. – JB Nizet Jul 05 '17 at 12:40
  • Of course you are right and what you say should fix this (and other similar) error, but my main point of interest is to find the reason of the two different results of this behaviour. – steve62742 Jul 05 '17 at 12:55
  • There is not enough code here to answer that. The behavior subject is hot and will fire synchronously so the error will be thrown immediately upon subscription. If this sometimes blocks other code from running and sometimes doesn't then it will depend on the scope in which `this.errors.subscribe` is called. Is it in an init method? A constructor? Some helper method? If you diff the two files what is different? The application is just a bunch of text files, no reason you can't diff them. – Pace Jul 05 '17 at 16:24
  • Also, see https://stackoverflow.com/questions/37836172/angular-2-doesnt-update-view-after-exception-is-thrown#answer-43796615 and https://stackoverflow.com/questions/35238428/angular2-application-crashes-becomes-unresponsive-after-encountering-an-except . Basically, if Angular 2 catches the error during its change detection loops, it will (in versions prior to 4.1.1) terminate the change detector loop. – Pace Jul 05 '17 at 16:26
  • Also, this really has little to do with behaviorsubject or rxjs. – Pace Jul 05 '17 at 16:28
  • you are most probably right, it might have nothing to do with the behaviorsubject. The two files are identical and the code snippet is on ngOnInit of a component. I must be something about the build/serve mechanism as thats the only things that can alter the behaviour of the same project ( although all the machines are running same node/npm/angular cli version, even the same terminal emulators ) – steve62742 Jul 06 '17 at 06:56

0 Answers0