0

I have a function, which makes query and processes it rezult in the subscription. This function must return 200 if query was successful or error codes in other cases. What I need to do to return value I got in the subscription?

public loginEmail(email: string, password: string): Subscription {
    return this.http.post < any > (`http://127.0.0.1:8080/user/`, {}, {
            params: new HttpParams().set('email', email).set('password', password)
        })
        .subscribe(resp => {
            console.log(resp);
            this.user.setUser(resp['user'])
            return 200;
        }, error => {
            return error.code
        });
}
Sebastian Richner
  • 782
  • 1
  • 13
  • 21
Pavlo
  • 166
  • 1
  • 11
  • I want to understand the use case that you had to rely on 200, rather than that, if you have something in your html template like *ngIf, which will render some conditional html component on the basis of response, you can place the condition on the user property of the component.......... – nobalG Dec 24 '20 at 16:09
  • This piece of code located in the service and returns value to the component. – Pavlo Dec 24 '20 at 16:13
  • 1
    return the user object as an observable, and subcribe to that observable in your component, why returning the status code of the http response then? – nobalG Dec 24 '20 at 16:24
  • Can't you just return new observable from the method? – Nikhil Patil Dec 24 '20 at 16:25
  • [See this](https://stackoverflow.com/q/47507179/681929) or [this](https://stackoverflow.com/a/55442329/681929) , I think if you will search this on google, 'how to return data from http request from a service to a component', you will get tons of results – nobalG Dec 24 '20 at 16:27

0 Answers0