0

I have a problem like this. I am making a mean stack application. In that file, I have created a service file to handle reservations. It looks like this.

import { Injectable } from '@angular/core';
import { HttpClient , HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import {Reservation} from "./reservation";

@Injectable()
export class ReservationService {
  public selectedReservation: Reservation = new Reservation();
  public  reservations: Reservation[];
  constructor(private http: HttpClient) { }

  getNotConfirmedReservationList(){
    return this.http.get('http://localhost:3000/reservation/notComfirmed')
  }

  confirm(_id: string){
    console.log(_id);
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    return this.http.post('http://localhost:3000/reservation/confirm', _id, {headers: headers});
  }

}

Here, I have put a console log it print the id correctly. But In the Controller file of my API which is made with Express, I again console log the Id it gives me output as undefined. Here I am providing my Controller file in the API.

const express = require('express');
var router = express.Router();

var  Reservation  = require('../models/reservation');

router.post("/create",function (req,res) {
    const newReservation = new Reservation({
        date: req.body.date,
        from: req.body.from,
        to: req.body.to,
        lab: req.body.lab,
        name: req.body.name,
        email: req.body.email,
        role: req.body.role,
        year: req.body.year,
        reason: req.body.reason,
        state: "Not Comfirm"
    });

    Reservation.saveReservation(newReservation, function (err, reservation) {
        if (reservation) {
            res.send(reservation);
        }

        else {
            console.log('Error in User save :' + JSON.stringify(err, undefined, 2));
        }
    });

});

router.get('/notComfirmed', function (req, res) {
    Reservation.findNotComfirmed(function (err, reservations) {
        if(err) throw err;

        if (!reservations){
            res.json({state:false,msg:"No reservations found"});
        }

        if(reservations){
            res.json(reservations);
        }

    })
});

router.post('/confirm', function(req, res){
    const _id= req.body._id;
    console.log(req.body._id);
    Reservation.updateReservation(_id, function (err, reservation) {
        if(err) throw err;

        if (!reservation){
            res.json({state:false,msg:"Something went wrong"});
        }

        if(reservation){
            res.json(reservation);
        }
    })
})


module.exports = router;

I tried a lot of times to find the issue with my code. But I was unable to find it. Can someone help me to find the problem with this code?. Thank you!

1
  • It looks like your POST body is just the ID, not an object with an _id property. Try logging req.body and see what you get Commented Jun 16, 2018 at 10:22

1 Answer 1

1
  1. You are not setting the request body correctly. Replace return this.http.post('http://localhost:3000/reservation/confirm', _id, {headers: headers}); by return this.http.post('http://localhost:3000/reservation/confirm', {_id}, {headers: headers});

  2. You are not parsing the request body. Replace router.post('/confirm', function(req, res) by router.post('/confirm', json(), function(req, res) and import const json = require('body-parser').json. Eventually you have to npm i --save body-parser.

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

1 Comment

I have already installed body-parser. I will check it an let you know if it works.

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.