angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

RxJS - cannot import operators. ( Observable has no method .map())

Open ev45ive opened this issue 10 years ago • 17 comments

Please provide us with the following information:

  1. OS? Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?) Windows 10 and MacOS X
  2. Versions. Please run ng --version. If there's nothing outputted, please run in a Terminal: node --version and paste the result here: node 6.5 npm 3.10
  3. 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)

  1. 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....

  1. 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.

ev45ive avatar Sep 01 '16 17:09 ev45ive

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

ghost avatar Sep 01 '16 22:09 ghost

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.

wulfsberg avatar Sep 02 '16 13:09 wulfsberg

@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 avatar Sep 03 '16 08:09 mikeybyker

@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.

filipesilva avatar Sep 21 '16 03:09 filipesilva

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.

tony2tones avatar Oct 09 '17 20:10 tony2tones

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?

YussufElarif avatar Feb 12 '18 11:02 YussufElarif

@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 avatar Feb 12 '18 16:02 wulfsberg

@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.

YussufElarif avatar Feb 14 '18 10:02 YussufElarif

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.

wulfsberg avatar Feb 14 '18 10:02 wulfsberg

Resolve with this imports:

import { Observable } from "rxjs";
import 'rxjs/add/operator/map';

M90K7 avatar Mar 13 '18 14:03 M90K7

Please check Below link: https://docs.google.com/document/d/12nlLt71VLKb-z3YaSGzUfx6mJbc34nsMXtByPUN35cg/edit#

i.e : npm install rxjs@6 rxjs-compat@6 --save

loveneetchahal avatar May 11 '18 10:05 loveneetchahal

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);
    });

scriptmaster avatar May 13 '18 08:05 scriptmaster

@lovesinghchahal Thanks! it works.

orassr avatar Jun 28 '18 18:06 orassr

If you using Angular 6, You must use .pipe rather .map

import { map } from 'rxjs/operators'; .pipe(result => result);

WhiteAngel2074 avatar Jul 06 '18 09:07 WhiteAngel2074

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

rubalIslam avatar Jul 07 '18 10:07 rubalIslam

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';

asmtony avatar Jul 15 '18 09:07 asmtony

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.