0

I am trying to build a MEAN stack application and i don't know why Angular is not sending any post request. When i try to post request using insomnia, it works fine. However its not the case with angular. Plus the issue lies in the LoginPlayer function. Rest of them are fine. web-service

import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import Player from 'src/models/Player';

@Injectable({
  providedIn: 'root'
})
export class WebService {
  player:Player
  readonly ROOT_URL;
  constructor(private http:HttpClient) {
    this.ROOT_URL='http://localhost:3000'
   }
   post(uri:string,payload:Player)
   {
     const obj={
      FullName:payload.FullName,
      Email:payload.Email,
      Password:payload.Password,
      Gender:payload.Gender,
     }
     return this.http.post(`${this.ROOT_URL}/${uri}`,obj);
   }
   LoginPlayer(uri:string,Email:String,Password:String)
   {
      const obj1={
          Email:Email,
          Password:Password
      }

     return this.http.post(`${this.ROOT_URL}/${uri}`,obj1);
   }

   get(uri:string){
     return this.http.get(`${this.ROOT_URL}/${uri}`)
   }
}

Player-Service

import { Injectable } from '@angular/core';
import { WebService } from './web.service';
import Player from 'src/models/Player';

@Injectable({
  providedIn: 'root'
})
export class PlayerService {
  Player:Player
  constructor(private WebService:WebService) { }
  createPlayer(player:Player){
    return this.WebService.post(`Players`,player)
  }
  getPlayers()
  {
    return this.WebService.get('Players')
  }
  LoginPlayer(Email:String,Password:String){
    return this.WebService.LoginPlayer(`players/Login`,Email,Password)
  }
}

server.js

const express = require('express')
const app = express()
const header = require('body-parser')
const router = express.Router()
const player = require('./Players')
const mongoose = require('./dbConfig')
app.use(express.json())
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,POST,HEAD,OPTIONS,PUT,PATCH,DEL");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});
app.listen(3000, () => console.log('Server started at 3000'))
app.post('/Players/Login', (req, res) => {
    player.find({ Email: req.body.Email, Password: req.body.Password })
        .then(player => console.log(player))
        .catch((error) => console.log(error));


});
app.post('/Players', (req, res) => {
    console.log(req.file);
    (new player({ 'Name': req.body.Name, 'Email': req.body.Email, 'Password': req.body.Password, 'Gender': req.body.Gender, 'Sports': req.body.Sports }))
    .save()
        .then((player) => console.log(player))
        .catch((error) => console.log(error))
});
app.patch('/Players/:Email', (req, res) => {
    player.findOneAndUpdate({ Email: req.params.Email }, { $push: { Sports: req.body.Sports } })
        .then(player => res.send(player))
        .catch((error) => console.log(error));
});
app.get('/Players', (req, res) => {
    player.find({})
        .then(player => res.send(player))
        .catch((error) => console.log(error));

});
1
  • 1
    You're not subscribing to the observable returned from the service. http calls won't fire unless you subscribe to them. Commented Apr 29, 2020 at 17:42

1 Answer 1

1

You are not subscribing to the Observable:

https://angular.io/guide/http#reading-the-full-response

What you should do in the component or service that is going to call the PlayerService is:

playerService.getPlayers().subscribe(
   (data) => {
    // Successs
    // Data is the result of your http request.
   } 

);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.