After upgrading to Angular 6.0 and Rxjs to 6.0 I receive the following compilation error:
Property 'do' does not exist on type 'Observable'.
Here is the code:
import { Observable, of } from 'rxjs';
import 'rxjs/add/operator/do';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import { IProduct } from './product';
@Injectable()
export class ProductService {
constructor(
private product: IProduct)
{
}
getProduct = () => {
return product.products
// error on next line
.do(data => console.log('All:' + JSON.stringify(data)))
.catch(this.handleError);
}
private handleError(err: HttpErrorResponse) {
console.log(err.message);
return Observable.throw(err.message);
}
}
Any idea?
The problem is not with angular but with rxjs. rxjs introduced breaking changes from rxjs version 6.
To get your code working again without changing any of your code install the following package:
npm install rxjs-compat@6 --save
You should then be able to compile your project.
rxjs-compat
is meant to be a temporarily solution so you need to update your codebase to work with the new version.New Import Path
What you need to update:
Update import statements from
import { Observable } from "rxjs/Observable";
to
import { Observable } from "rxjs";
Update your operator imports from
import 'rxjs/add/operator/do'
to
import { do } from "rxjs/operators";
Renamed Operators
Some operators have also been renamed due to name collisions with JavaScript reserved words. They are
do
=>tap
catch
=>catchError
switch
=>switchAll
finally
=>finalize
No Operator Chaining
You also then can't chain your operators anymore you need to use the
pipe
operator e.g.Rxjs 6 has introduced some breaking changes and the "do" operator has been replaced by the "tap" operator (from '
rxjs/internal/operators
').You could refactor your code using the new operator or still use the old 'do' syntax by adding the rxjs-compat library for backward compatibility (
npm install --save rxjs-compat
).Note that before RxJs 6 you had to import the 'do' operator :
More details here : Angular HTTP GET with TypeScript error http.get(...).map is not a function in [null]
Just use in your typescript file:
as simple as that. Thanks.
I appreciate Tjaart van der Walt's response about how to resolve the "breaking changes" introduced in Angular/rxjs7++. But I still encountered several problems trying to apply his response to my Angular interceptor:
Here is the updated code (the sections that failed to compile are marked "OLD")
Required changes include changing
import
paths, and substitutingpipe()
,tap()
andof()
.This link is also a good resource for RxJS6 changes:
https://www.academind.com/learn/javascript/rxjs-6-what-changed/