-1

I am using HttpModule to call my API. I have included the module in my imports and while starting my server an error is thrown(pl look at the screeshot). Error Image

I am trying to call my API from the service using Http.post and getting the error shown above

My app.module.ts:

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';

import {AppComponent} from './app.component';
import {HeaderComponent} from './header/header.component';
import {AuthComponent} from './auth/auth.component';
import {MarksComponent} from './marks/marks.component';
import {SigninComponent} from './auth/signin/signin.component';
import {SignupComponent} from './auth/signup/signup.component';
import {FormsModule} from '@angular/forms';
import {AppRoutingModule} from './app-routing.module';
import {AuthService} from './auth/auth.service';
import {HttpModule} from '@angular/http';

@NgModule({
declarations: [
AppComponent,
HeaderComponent,
AuthComponent,
MarksComponent,
SigninComponent,
SignupComponent
 ],
imports: [
BrowserModule,
FormsModule,
AppRoutingModule,
HttpModule
],
providers: [AuthService],
 bootstrap: [AppComponent]
})
export class AppModule {
}

My service:

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

@Injectable()
export class AuthService {
  constructor(private router: Router, private http: Http) {

  }
  signUpUser(firstName: string, lastName: string, email: string, password: 
  string) {
       const body: [] = [firstName, lastName, email, password];
       return this.http.post('http://localhost:3000/auth/signup', body).map(data 
    => {
            data = data.json();
            return data;
        });
    }
 }
Sébastien
  • 11,860
  • 11
  • 58
  • 78
user8885733
  • 137
  • 2
  • 13
  • Could you post your error ? My proxy doesn't allow imgur, I can't see it ... thank you ! –  Dec 21 '17 at 10:03

3 Answers3

0

In my opinion it's not a http problem but a bad declaration :

use this to define body :

let body = {conge,user};
 this.httpClient.post('http://localhost:8080/api/sendconge', body)
.subscribe(function(data) {
        console.log(data);
});

httpClient is better to send data than simple http.

0

I was using the post method in a wrong way. My updated code is:

signUpUser(firstName: string, lastName: string, email: string, password: string) {
// const body: [] = [firstName, lastName, email, password];
const body = {
    firstName: firstName,
    lastName : lastName,
    email : email,
    password : password
  };
return this.http.post('http://localhost:3000/auth/signup', body);

}

user8885733
  • 137
  • 2
  • 13
-1

you should give a type to body array.

for eg:

const body: any[] = [firstName, lastName, email, password];

Gautam
  • 1,046
  • 5
  • 20