Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't seem to use 'first()' on Observable(rxjs in Angular)

I read about this awesome feature in RXJS which would theoretically allow me to subscribe only for one response, but can't seem to implement it. Instead, I'm getting this error,

"Property 'first' does not exist on type 'Observable'."

although I am just copy-pasting from existing examples. Here's my code:

implementHash( token ){
    console.log('trying to implement hash', token);
    const req = this.http.get('http://localhost:3003/token/' + token );
    const sub = req
        //.first()
        .subscribe( res =>{
            sub.unsubscribe; // trying to avoid this line
    });
};
like image 762
Efim Rozovsky Avatar asked Mar 08 '23 00:03

Efim Rozovsky


2 Answers

You need to

import 'rxjs/add/operator/first';

in your file.

like image 82
alexKhymenko Avatar answered Mar 20 '23 02:03

alexKhymenko


Add import 'rxjs/add/operator/first' to your component.ts file.

It comes from the import first.

like image 43
Sajeetharan Avatar answered Mar 20 '23 02:03

Sajeetharan