1

I'm new to Angularjs. I need to send post request to my localhost server. But I got an error [ts] cannot find name 'map' and cannot find name 'subscribe'. i hope will understand my error . Thanks in advance.

login.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Http ,Headers ,HttpModule} from '@angular/http';
import 'rxjs/add/operator/map';

/**
 * Generated class for the Login page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class Login {

  constructor(public navCtrl: NavController, public navParams: NavParams,public http:Http) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad Login');
  }
  ionViewidData(){
    let headers= new Headers();
    headers.append('Content-Type','application/json');

    let body = {
      device:"Mobile",
      id:"10077",
      password:"leechan2"
    };
    console.log(body);
    this.http.post('http://0.0.0.0:2800/login',JSON.stringify(body),{headers:headers});
        .map(res => res.json())
        .subscribe(data =>{
          console.log(data);
        });

}

}

4 Answers 4

2

you have an unexpected ; at line this.http.post.

this.http.post('http://0.0.0.0:2800/login',JSON.stringify(body),{headers:headers})           //    <--------- remove ; here
    .map(res => res.json())
    .subscribe(data =>{
      console.log(data);
    });
Sign up to request clarification or add additional context in comments.

Comments

2

**Remove ; after this.http.post and try to use private http:Http in constructor **

this.http.post('http://0.0.0.0:2800/login',JSON.stringify(body),{headers:headers})
    .map(res => res.json())
    .subscribe(data =>{
      console.log(data);
    });

Comments

2

You should import it before using:

import { map } from 'rxjs/operators';

This works for me.

Comments

0

Syntax error can sometimes give strange errors which might not point directly to your root cause. You finished your 'post' method with a ';' so your 'map' method is not called on any object. Nothing on the left side of the '.' is causing the 'map' method not to be found. Remove the ';' after 'headers});'.

Like this: this.http.post('http://0.0.0.0:2800/login',JSON.stringify(body),{headers:headers}).map(res => res.json()).subscribe(data =>{ console.log(data); });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.