Thabiso Mofokeng
Thabiso Mofokeng

Reputation: 749

rxjs__WEBPACK_IMPORTED_MODULE_1__.Observable.throw is not a function

I am using Angular 6 with rxjs^6.0.0 and rxjs-compat^6.5.2 but I am getting the following error which I can't resolve:

rxjs__WEBPACK_IMPORTED_MODULE_1__.Observable.throw is not a function

I have tried the following solutions with no success:

This is the code that I'm using:

import { Injectable, Inject } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { BehaviorSubject, throwError } from 'rxjs';
import { Observable } from 'rxjs/Observable';
import { map, tap, mergeMap } from 'rxjs/Operators';
import { Login } from "./login/login";
import { CurrentUser } from "./login/current-user";
import { Registration } from "./registrations/registration";

@Injectable()
export class AuthenticationService {
  private currentUserSubject: BehaviorSubject<CurrentUser>;
  public currentUser: Observable<CurrentUser>;

  constructor(private httpClient: HttpClient,
    @Inject("BASE_API_URL") private baseUrl: string) {
    this.currentUserSubject = new BehaviorSubject<CurrentUser>(JSON.parse(localStorage.getItem('currentUser')));
    this.currentUser = this.currentUserSubject.asObservable();
  }

  public get currentUserValue(): CurrentUser {
    return this.currentUserSubject.value;
  }

  signIn<T>(login: Login) {
    return this.httpClient.post<T>(`${this.baseUrl}api/authentication/login`, login)
      .pipe(mergeMap(loginResult => {
        return this.httpClient.get<Registration>(`${this.baseUrl}api/users/${loginResult.user.id}/registration`)
          .pipe(map(registration => {
            if (registration.registrationStatusId == 1)
              return throwError('Registration is pending approval.');
            else if (registration.registrationStatusId == 3)
              return throwError('Registration has been rejected.');

            let currentUser = new CurrentUser();
            currentUser.identity = registration;
            currentUser.identity.user = loginResult.user;
            currentUser.token = loginResult.token;
            currentUser.refreshToken = loginResult.refreshToken;

            localStorage.setItem('currentUser', JSON.stringify(currentUser));
            this.currentUserSubject.next(currentUser);
          }));
      }));
  }
}

Upvotes: 3

Views: 2071

Answers (1)

Tony
Tony

Reputation: 20162

Your import code is correct but I think you should use same version for rxjs and rxjs-compact so update your package.json

"rxjs": "^6.5.2",
"rxjs-compat": "^6.5.2"

Then delete package-lock.json then run npm install again

Upvotes: 1

Related Questions