Tom
Tom

Reputation: 8691

subscribe does not exist on type observable<any>

I have upgraded my application from angular 4 to angular 6. I am getting error subscribe does not exist on type observable . Could somebody tell me if anything has changed in angular 6

import { Injectable } from '@angular/core';
import { Dto, ApiResult } from '../api';
import { RunsProxy } from '../api/proxies';
import { Observable, ReplaySubject, Subject } from 'rxjs';
import { AlertService } from './alert.service';
import { TranslateService } from '@ngx-translate/core';
import { ReadonlyProvider } from '@wtw/toolkit/src/directives/read-only-inputs.directive';
import { Router, NavigationStart, NavigationCancel, NavigationEnd } from '@angular/router';
import { CurrencyInfo, RunExecution } from '../api/dtos';
import { tap , map, share, delay } from 'rxjs/operators'
import { fireAndForget } from "platform/tests/helpers";

     *public load(id: number): Observable<ApiResult<Dto.RunModel>> {
            const obs = this._runs.get(id).uiSignal('load run').share();
            obs.subscribe(ret => {
                if (!!!ret.data && this.blnShown === false) {
                    this.blnShown = true;
                    this._translate.get('GLOBAL.TOASTS.RUN_UNAVAILABLE').subscribe(o => {
                        this._alertService.error(o);
                    });
                }
                this._activeRun.next(ret.data);
            }, err => {
                if (err.status === 403 || err.status === 404) {
                    this._router.navigate(['/home']);
                    this._alertService.clear();
                    this._translate.get('GLOBAL.TOASTS.RUN_UNAVAILABLE').subscribe(o => this._alertService.error(o));
                } else throw err;
            });
            return obs;
        }*

Upvotes: 0

Views: 346

Answers (1)

Aarsh
Aarsh

Reputation: 2605

Here is an example of my service. You can put same code as per your requirement.

My Service

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})

export class PollSerivce {

  constructor(private _http: Http) { }

  getPollPostData(url: any) {
    const options = new RequestOptions({ withCredentials: true });
    return this._http.get(url)
      .pipe(
        catchError(this.handleError)
      );
  }

  private handleError(error: Response) {
    console.error(error);
    return Observable.throw(error.json().error || 'Server error');
  }
}

My Component file

getDataSource() {
    this._pollSubscription = this.pollSerivce.getPollPostData(StaticKeywords.baseUrl).subscribe(response => {
      this.pollPostData = response.json().hits;
    },
      err => {
        console.log(err);
      });
  }

There are many changes in rxjs 6 so if you want to learn more about it you can check this websites ::

https://auth0.com/blog/whats-new-in-rxjs-6/

https://www.academind.com/learn/javascript/rxjs-6-what-changed/

https://www.learnrxjs.io/concepts/rxjs5-6.html

Upvotes: 1

Related Questions