-1

Property 'interval' does not exist on type 'typeof Observable'. Even though its imported in and I dont know why it wont work. Ive seen some posts about this involving angular 6 but this is angular 11 so I hope someone can help me figure this out.

import { Component, OnInit } from '@angular/core';
import { TokenService } from '../../authentication/services/token.service';
import { Router } from '@angular/router';
import { HttpErrorResponse } from '@angular/common/http';
import { AdminService } from '../../authentication/services/admin.service';
import { CrudService } from '../services/crud.service';
import { Observable } from 'rxjs';
import { switchMap, startWith } from 'rxjs/operators';
@Component({
  selector: 'app-admin-dashboard',
  templateUrl: './admin-dashboard.component.html',
  styleUrls: ['./admin-dashboard.component.css'],
})
export class AdminDashboardComponent {
  constructor(
    private _token: TokenService,
    private _router: Router,
    private _admin: AdminService,
    private _crud: CrudService
  ) {}

  adminId: string;
  adminName: string;
  adminEmail: string;
  userCount$: Observable<any>;
  userCount: string;

  ngOnInit(): void {
    this._token.verifyToken().subscribe(
      (res) => {
        this.adminId = res.admin._id;
        localStorage.setItem('adminid', this.adminId);
        this._admin.getAdminById(this.adminId).subscribe((res) => {
          this.adminName = res.admin.name;
          this.adminEmail = res.admin.email;
          // this._crud.getUserCount().subscribe((count) => {
          //   this.userCount$ = count.count;
          // });
          this.userCount$ = Observable.interval(1000)
            .startsWith(0)
            .switchMap(() => {
              this._crud.getUserCount().subscribe((count) => {
                this.userCount = count.count;
              });
            });
        });
      },
      (err) => {
        if (err instanceof HttpErrorResponse) {
          if (err.status === 400) {
            this._router.navigate(['/login']);
          }
        }
      }
    );
  }
}

lizardcoder
  • 336
  • 3
  • 6
  • Also I know interval is not imported but i already imported it before and it still wasent working – lizardcoder Jan 12 '21 at 23:31
  • Sounds like you're trying to mix the "old" way and the "new" way. Since rxjs version 6 (I think, fact check it) you need to import the individual operators and pipe them through, not attach them to the observable directly. Look up "lettable" or "pipeable" operators. – Phix Jan 12 '21 at 23:36
  • What error do you get if you import `interval` and use `interval` directly instead of `Observable.interval`? – ShamPooSham Jan 12 '21 at 23:39
  • @Phix I will try that write now – lizardcoder Jan 13 '21 at 00:04

2 Answers2

0

Try this:

import { interval } from 'rxjs';

....
interval(1000).startWith.... // Change Observable.interval to interval and import it from rxjs.
AliF50
  • 16,947
  • 1
  • 21
  • 37
0

Its solved now I just forgot to use pipe

lizardcoder
  • 336
  • 3
  • 6