2

Current behavior

I'm getting the following exception when I pass up an observable over 2 methods:

TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at HttpRequest.serializeBody (http.js:916)
    at Observable._subscribe (http.js:2196)
    at Observable._trySubscribe (Observable.js:172)
    at Observable.subscribe (Observable.js:160)
    at Object.subscribeToResult (subscribeToResult.js:23)
    at MergeMapSubscriber._innerSub (mergeMap.js:132)
    at MergeMapSubscriber._tryNext (mergeMap.js:129)
    at MergeMapSubscriber._next (mergeMap.js:112)
    at MergeMapSubscriber.Subscriber.next (Subscriber.js:90)

If I call the method post2 (see below) directly in the login function then everything works and I see the success message.

Expected behavior

No exception should occur.

Minimal reproduction of the problem with instructions

I have the following template & method: <button (click)="login()">Click me</button>

inside the login method:

public login(): void {
  this.webService
    .post('auth/login', { username: this.username, password: this.password })
    .subscribe((data: any) => {
       console.log('sucess');
     }, (err: any) => {
        console.log('errror');
  });
}

And then in the web service:

public post(url: string, body: any): Observable<any> {
  return this.post2(url, body);
}

and the 2nd method:

public post2(url: string, body: any): Observable<any> {
  return this.http.post('/auth/login', body);
}

If I call the method post2 directly in the login function then everything works and I see the success message.

What is the motivation / use case for changing the behavior?

Environment

Angular version: 5.2.7

Browser: - [X] Chrome (desktop) version 66

Marc Borni
  • 384
  • 1
  • 17
  • 1
    I tried the same code, it's working for me. No error. Maybe you are looking at the wrong snippet of code. Have you checked, if you have any circular JSON or something like that? check here, https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json/11616993 – Amaresh C. May 11 '18 at 10:08
  • hmm so it seems that something is wrong with my project. That might be the case. Thanks for checking it out @AmareshC. ! – Marc Borni May 12 '18 at 12:02
  • I'm guessing the problem is with the return value of the `'/auth/login'` endpoint. Could you show what is returned from the server? – Krypt1 May 12 '18 at 12:47
  • Normal JSON is returned from the endpoint. – Marc Borni May 15 '18 at 11:30

0 Answers0