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!
_idproperty. Try loggingreq.bodyand see what you get