Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 http is missing .map function [duplicate]

I found this example code in a tutorial:

getRandomQuote() {
  this.http.get('http://localhost:3001/api/random-quote')
    .map(res => res.text())
    .subscribe(
      data => this.randomQuote = data,
      err => this.logError(err),
      () => console.log('Random Quote Complete')
    );
}

But when trying to use it, I only get TypeError: this.http.get(...).map is not a function in [null]:

getChannels():Promise<Channel> {
    return this.http.get('...')
        .map(function (response:Response) {
            ...
        }).toPromise();
}

My Typescript compiler tells me that those methods are avaible but when inspecting the return value of http.get() they are missing.

I used the package.json of the current angualar2 starting guide:

"dependencies": {
  "angular2": "2.0.0-beta.0",
  "systemjs": "0.19.6",
  "es6-promise": "^3.0.2",
  "es6-shim": "^0.33.3",
  "reflect-metadata": "0.1.2",
  "rxjs": "5.0.0-beta.0",
  "zone.js": "0.5.10"
},

...

<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>

Any ideas what I might get wrong at this point?

like image 724
K. D. Avatar asked Jan 03 '16 20:01

K. D.


People also ask

How to use the map operator in angular?

Using Map operator in Angular Syntax. The syntax of the map operator is as follows. The project can accept two arguments. one is value i.e. the value... Using Observable Map. To use map first we need to import it from the rxjs/operators library. Next, create an observable... Map Examples. The ...

What happened to the @angular/HTTP module?

The @angular/http module has been removed in recent versions of Angular and replaced by @angular/common/http . Furthermore, there are some name changes that might need manual fixing, including:

How to handle multiple HTTP requests in angular?

There are multiple ways to handle multiple requests; they can be sequential or in parallel. In this post, we will cover both. Let's start with a simple HTTP request using the Angular Http service. In our app, we have just a single component that pulls in Angular's Http service via Dependency Injection.

Can subscribe be missing from an observable in Angular 2?

This at least proves that you are getting back an observable with the correct data. subscribe is not considered an operator, but rather a method of any observable instance. Thus it can never be missing. Sorry, something went wrong. IMO This really needs to be added to the angular2 documentation ( import rxjs/Rx or importing operators 1 by 1).


2 Answers

Observable by default comes with just a few operators. You have to explicitly import them:

import 'rxjs/add/operator/map';

or, if you don't wanna think about it, just load everything (in your bootstrap file, for example):

import 'rxjs/Rx';
like image 137
Sasxa Avatar answered Oct 19 '22 13:10

Sasxa


You will want your index.html to look something like this, so system.js can find all the rxjs dependencies that you import in your components.

        <script src="/lib/anguar2/angular2-polyfills.js"></script>
        <script src="/lib/es6-shim/es6-shim.js"></script>
        <script src="/lib/systemjs/system.src.js"></script>

        <script>
            System.config({
                defaultJSExtensions: true,
                packages: {
                    app: {
                        format: 'register'
                    }
                },
                map: {
                    'rxjs':"lib/rxjs"

                }
            });
        </script>
        <script src="/lib/anguar2/angular2.dev.js"></script>
        <script src="/lib/anguar2/router.dev.js"></script>
        <script src="/lib/anguar2/http.js"></script>
        <script>
            System.import('app/boot');
        </script>
Make sure the "lib/rxjs" folder has ALL the files from the node_modules/rxjs folder. Not all of them will be loaded, only the ones you need and their dependencies (system.js will figure this out for you).

Now you can use this in your boot.ts file:

import 'rxjs/add/operator/map';
like image 39
brando Avatar answered Oct 19 '22 12:10

brando