Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - http.post(...).map is not a function [duplicate]

Tags:

angular

rxjs

I've looked up all the github issues, and the StackOverflow posts, but I'm unable to get it working

( https://github.com/angular/angular/issues/5632 )

(Angular 2 HTTP GET with TypeScript error http.get(...).map is not a function in [null] )

I tried different imports:

import 'rxjs/add/operator/map';

import 'rxjs/rx';

But I keep getting the error http.post(...).map is not a function

update - code context

let body = "email=" + email + "&password=" + password;
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-from-urlencoded');
this.http.post('http://angular.app/api/v1/auth') // angular.app is laravel backend
      .map( (responseData) => {
          return responseData.json();
      })
like image 813
Angelo A Avatar asked Jan 25 '16 08:01

Angelo A


2 Answers

For me the http.post(...).map() works as expected. I do need the import 'rxjs/Rx'

import {Component} from 'angular2/core';
import {Http} from 'angular2/http';
import 'rxjs/Rx';
@Component({
  selector: 'my-app',
  template: `
    <h1>{{title}}</h1>
    <p>Test result {{result | json}}</p>
    `
})
export class App implements OnInit{
 public title = 'my title';
 public result : String;

constructor(private _http : Http) {
  _http.post('http://jsonplaceholder.typicode.com/posts')
    .map(res => res.json())
    .subscribe(
      data => this.result = data,
      err => console.log('ERROR!!!'),
      () => console.log('Got response from API', this.result)
    );
  }
}

See plunker example: http://plnkr.co/edit/vise2zYxZUmr1kW65mNY?p=preview

hopes this will help you to find your porblem

like image 158
Stefan van de Vooren Avatar answered Nov 03 '22 16:11

Stefan van de Vooren


It seems that Angular2 beta.1 requires RxJS 5.0.0-beta.0. Perhaps it's the cause of your problem.

If I try this in my package.json file:

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

And I have the error that Angular2 requires RxJS 5.0.0-beta.0.

Edit

You need to add the HTTP_PROVIDERS within the second parameter of your bootstrap function.

Hope it helps you, Thierry

like image 27
Thierry Templier Avatar answered Nov 03 '22 16:11

Thierry Templier