http post error

Asked

Viewed 85 times

1

Friends, I would like to have the save method in the class itself, and I’m not getting it, I’m beginner in typescript.

    import { Injectable }     from '@angular/core';
    import { Http } from '@angular/http';


    @Injectable()

    export class Usuario {

      http: Http;
      nome: string;
      apelido: string;
      email: string;
      tel: string;
      senha: string;

      salvar(usuario){
        this.http.post("http://localhost:5000/users",JSON.stringify(usuario), {})
                 .toPromise()
                 .then(res => console.log(res.json().data))
                 .catch();
      }

    }

get the bug:

    browser_adapter.js:84 ORIGINAL EXCEPTION: TypeError: Cannot read property 'post' of undefined
  • Have you looked before chmod (type permission)? it is saying error Undefined is empty.

  • already yes, permissions are normal.

  • It seems that he is not accepting his "this.http". All the examples I saw and in a code I made http arrow in the constructor, I haven’t had time to test yet, but I use constructor(private http:Http) {}. Even empty as you can see on this link http://www.gajotres.net/ionic-2-making-rest-http-requests-like-a-pro/

  • exactly @Andrévicente, but when I try to instantiate a User object, "Let usuario = new User()" is the error, because I asked to pass http as a parameter in the constructor. My idea is to set the attributes in the User object and save the same in the database.

1 answer

0


You forgot to inject the Http.

constructor(private http: Http) {
  }

And then on your AppModule

import { HttpModule } from '@angular/http';

...

 imports:[
  HttpModule,
],
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.