Nuno Peralta
Nuno Peralta

Reputation: 25

Type 'Observable<boolean>' is not assignable to type 'Observable<UserRegistration>'

register(email: string, password: string, firstName: string, lastName: string,location: string): Observable<UserRegistration> {
    let body = JSON.stringify({ email, password, firstName, lastName,location });
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post(this.baseUrl + "/accounts", body, options)
      .map(res => true)
      .catch(this.handleError);
  }

I found this code online, and i'm trying to implement it in my own project, but i keep getting the follow error:

Type 'Observable<boolean>' is not assignable to type 'Observable<UserRegistration>'.
  Type 'boolean' is not assignable to type 'UserRegistration'.

it also uses this class:

export interface UserRegistration {
  email: string;
  password: string;
  firstName: string;
  lastName:  string;
  location: string;
}

Upvotes: 1

Views: 816

Answers (5)

Salman Sarray
Salman Sarray

Reputation: 1437

your return type is Observable<UserRegistration> but you're returning a boolean you should either change the return type to Observable<boolean> or return an object of type Observable<UserRegisteration>

Welcome to Stackoverflow:)

Upvotes: 0

Eliya Cohen
Eliya Cohen

Reputation: 11478

The error is pretty obvious. You told Typescript you expect from the method to return an observation of UserRegistration while you mapped and returned your result to Boolean on this line of code:

.map(res => true)

According to your code - if there an error won't be thrown from the http request, then map the result to "true" and return it.

Just change the value from in the first line from Observable<UserRegistration> to Observable<boolean>

Upvotes: 2

saravana manikandan
saravana manikandan

Reputation: 191

if you are returning with UserRegistration object then, use below code. I have altered little

register(email: string, password: string, firstName: string, lastName: string,location: string): Observable<UserRegistration> {
    let body = JSON.stringify({ email, password, firstName, lastName,location });
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post(this.baseUrl + "/accounts", body, options)  
      .catch(this.handleError);
  }

Upvotes: 1

DeborahK
DeborahK

Reputation: 60528

Welcome to stackoverflow!

Your map statement is returning true:

return this.http.post(this.baseUrl + "/accounts", body, options)
  .map(res => true)
  .catch(this.handleError);

I assume you really want to be returning the observable. If so, you don't need to map it.

What version of Angular and RxJS are you using?

If you are using the current version of Angular (v7) then it should look like this:

return this.http.post(this.baseUrl + "/accounts", body, options).pipe(
  catchError(this.handleError)
);

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222582

As your method shows , you are returning a boolean true , while your definition says it should return Observable<UserRegistration>. Change your method return type as,

register(email: string, password: string, firstName: string, lastName: string,location: string): Observable<boolean>

However your logic does not seem to be right as it always going to return true.

Upvotes: 2

Related Questions