RxJS - cannot import operators. ( Observable has no method .map())
Please provide us with the following information:
- OS? Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?) Windows 10 and MacOS X
- Versions. Please run
ng --version. If there's nothing outputted, please run in a Terminal:node --versionand paste the result here: node 6.5 npm 3.10 - Repro steps. Was this an app that wasn't created using the CLI? What change did you do on your code? etc. tried npm install angular-cli angular-cli@webpack [email protected] In each case next: ng init ng serve
and tried to import Rxjs: import 'rxjs' import 'rxjs/Rx' import { Observable } from 'rxjs'; import 'rxjs/add/operator/map'
but using for example Http like: this.http.get(url).map(smth2).subscribe(smth2)
- The log given by the failure. Normally this include a stack trace and some more information.
and i get Observer has no method .map()
or it tries to load 101 GET http://localhost:4200/vendor/rxjs/index.js 404 (Not Found) which makes no sense to me....
- Mention any other details that might be useful.
Basically - how do I extend every Observable / EventEmmiter in Angular produced by angular-cli so i can import and use RxJS operators??? :-)
Thanks! We'll be in touch soon.
I don't exactly know what you want to achieve, but you should call subscribe on get. Not sure what you're trying to map, but do so inside of subscribe
In code:
this.http.get(url).subscribe((data) => { doStuff(data) })
As you can see, the error message says that Observer doesn't have a map method. You can only call map on a Observable. Note the difference
I think you need to
import { Observable } from 'rxjs/Rx';
i.e. from rxjs/RX rather than just rxjs.
I am not entirely sure why it shows up under both imports, and what the difference between them actually is, but it has given me some grief in the past.
@ev45ive - I don't think there's an issue here...
You are getting
http://localhost:4200/vendor/rxjs/index.js 404 (Not Found)
from your
import 'rxjs';
You should get rid of that line - use instead:
import 'rxjs/Rx';
that will give you .map - and also everything else...
If it is just the map operator you want, just keep the line
import 'rxjs/add/operator/map';
You can then use map on the observable just like a standard array map - so something like:
this.http.get(this.url).map(res => res.json()).subscribe(items => console.log(items));
@mikeybyker is correct, you should be able to just get the map operators. See https://angular.io/docs/ts/latest/tutorial/toh-pt6.html#!#import-rxjs-operators for more information about importing RxJS operators.
I just check my version of npm, and just typed in npm update, after a bit of time i could get a result for ng --version. Hope this helps.
In the project, where would be the best place to put these imports?
Using the import 'rxjs/add/operator/map, should that be imported only once in the project or multiple times under each and every component, service and test that require it?
@vemuez I'd recommend putting it in every file which uses it. Otherwise, you risk that your code suddenly breaks because somebody removed a "magic import" from somewhere else in the project.
But this issue of imports randomly impacting other code is in fact one of the major reasons for moving to pipeable operators. The augmentation import style has practically shown to be dangerously brittle.
@wulfsberg If you were to only place it in one place, the entry point of the application such as main.ts, wouldn't that reduce the build time and should be self explanatory that those imports are of use in the application in such a case where someone was about to remove it?
Thanks in advance.
Possibly, but then you end up with a file which slowly accumulates cruft, and which nobody can ever remove anything from, because it might break something deep down in some component.
In general, the whole idea with imports is that a code block says, "I need this to be able to run". Using a style where "this code block can only run if somebody else has swung a dead cat over their head somewhere else in the code" is just too brittle.
Resolve with this imports:
import { Observable } from "rxjs";
import 'rxjs/add/operator/map';
Please check Below link: https://docs.google.com/document/d/12nlLt71VLKb-z3YaSGzUfx6mJbc34nsMXtByPUN35cg/edit#
i.e : npm install rxjs@6 rxjs-compat@6 --save
In rxjs 6+:
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
this.http.get(url)
.pipe(map(r => r.json()))
.subscribe(resp => {
resp = resp.json();
console.log(resp);
});
@lovesinghchahal Thanks! it works.
If you using Angular 6, You must use .pipe rather .map
import { map } from 'rxjs/operators'; .pipe(result => result);
instead of using
import 'rxjs/add/operator/map'; this.obObservable().map(data => {})
use
import { map } from "rxjs/operators"; this.obObservable().pipe(map(data => {}))
angular changes it recently
Yeah this worked for me. Using Angular 6 Old import { Observable } from "rxjs"; import 'rxjs/add/operator/map';
Replaced with import { Observable } from 'rxjs'; import { map } from 'rxjs/operators';
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.