Kery Hu
Kery Hu

Reputation: 5906

angular2 Observable Property 'debouceTime' does not exist on type 'Observable<any>'

I use "angular2 webpack" and "angular2/form,Observable" , but met an error ,need help ..

There is a custom form validator --

import {Observable} from 'rxjs/Rx';
import {REACTIVE_FORM_DIRECTIVES,FormControl, FormGroup, Validators} from '@angular/forms';

emailShouldBeUnique(control:FormControl) {
    return new Observable((obs:any)=> {
      control.valueChanges
        .debouceTime(400)
        .distinctUntilChanged()
        .flatMap(term=>return !this.userQuery.emailExist(term))
        .subscribe(res=> {
            if (!res) {obs.next(null)}
            else {obs.next({'emailExist': true}); }; }
        )});}

I could find the file "/projection_direction/node_modules/rxjs/operator/debounceTime.js"

why is there such the error--

Property 'debouceTime' does not exist on type 'Observable'.

Upvotes: 21

Views: 27985

Answers (8)

Manohar Reddy Poreddy
Manohar Reddy Poreddy

Reputation: 27465

Aug 2019

An example:

Imports:

import { Subject } from "rxjs";
import { debounceTime } from "rxjs/operators";

In component class:

resizeEvent: Subject<any> = new Subject<any>();

@HostListener("window:resize", ["$event"])
onResize(event: any) {
  this.resizeEvent.next(event);
}

ngOnInit() {
  console.log("in ngOnInit");

  this.resizeEvent
    .pipe(debounceTime(500))
    .subscribe(this.actualResizeHandler);
}

actualResizeHandler(event: any) {
  //   event.target.innerWidth;
  console.log("in actualResizeHandler");
}

Upvotes: 1

rawatDigamber
rawatDigamber

Reputation: 11

Let's say you have to use debounceTime() with multiple RxJS operators, I would suggest use .pipe() operator

import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';

 functionName(value: Observable<string>) {
    return .pipe(
      debounceTime(400),
      distinctUntilChanged(),
      switchMap(location => this.secondFunc(value))
    )
  }

Upvotes: 0

Aman
Aman

Reputation: 177

I had same problem recently, and solved after I did:

import { debounceTime } from 'rxjs/operators';

And also added pipe as it is required I think for Angular 5+

something.pipe(debounceTime(100)).subscribe(something...);

Upvotes: 0

DHLopez
DHLopez

Reputation: 395

For me, the answer was using the pipe:

.pipe(debounceTime(500))

Plus changing the import from:

import "rxjs/add/operator/debounceTime"; 

To:

import {debounceTime} from 'rxjs/internal/operators';

And yes, I was following a tutorial, so hopefully this helps

Upvotes: 3

user3249027
user3249027

Reputation: 542

For everyone coming here after rxjs 6:

You now need to use a pipe():

What was

myObservable$
    .debounceTime(500)
    .subscribe(val => {
    // debounced stuff
})

needs now to be:

myObservable$
    .pipe(debounceTime(500))
    .subscribe(val => {
    // debounced stuff
})

https://www.learnrxjs.io/operators/filtering/debouncetime.html

Upvotes: 34

GeorgeAnagnostopoulos
GeorgeAnagnostopoulos

Reputation: 63

I recently had a similar error while working with angular v5.2.6 and rxjs v5.5.6 on an angular-cli 1.6.8 generated project. I originally had:

import { debounceTime, map } from 'rxjs/operators;

since I was subscribing for a control valueChanges event and I kept getting the error until I put

import { Observable } from 'rxjs/Rx';

I hope this helps!

Upvotes: 3

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123891

Be sure you've initiated that in main.ts (where the app is bootstraped)

import "rxjs/add/operator/map";
import "rxjs/add/operator/debounceTime";
...

or all at once

import "rxjs/Rx";

EXTEND

there is a working example

//our root app component
import {Component, EventEmitter, ChangeDetectorRef} from '@angular/core'
import {Observable} from  "rxjs/Rx";
@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>

        <div>debounced message: {{message}}</div>
    </div>
  `, 
  directives: []
})
export class App {

  protected message: string;
  protected emitter = new EventEmitter<string>();
  public obs: Observable<string>;

  constructor() {
    this.name = 'Angular2 (Release Candidate!)'

    this.obs = this.emitter
      .map(x => x)
      .debounceTime(1200)
      ;

    this.obs.subscribe(msg => this.message = msg);
  }

  ngOnInit(){
    this.emitter.emit("hello after debounce");
  }
}

and that is working when in main.ts we have:

//main entry point
import {bootstrap} from '@angular/platform-browser-dynamic';
import {App} from './app';

import "rxjs/Rx"; 

bootstrap(App, [])
  .catch(err => console.error(err));

Check it here

Upvotes: 45

Can Nguyen
Can Nguyen

Reputation: 1470

You have a typo here. It's debounceTime, not debouceTime :)

Upvotes: 4

Related Questions