How to fix this error ?
Here is HotelRoom Module
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const HotelRoomSchema = new Schema({
name : {
type : String,
require : true
},
numberofRooms : {
type : Number,
require : true
},
typeOfBeds : {
type : String,
require : true
},
noOfBeds : {
type : Number,
require : true
},
sleepers : {
type : Number,
require : true
},
view : {
type : String,
require : true
},
area : {
type : Number,
require : true
},
price : {
type : Number,
require : true
},
hotel : {
type :Schema.Types.ObjectId ,ref:"Hotel",require:true
}
})
const HotelRoom = mongoose.model("HotelRoom",HotelRoomSchema);
module.exports = HotelRoom;
/////////Here is the route
router.route("/:hotelId/addroom").post((req,res) =>{
let hotelId = req.params.id;
const name = req.body.name;
const numberofRooms = Number(req.body.numberofRooms);
const typeOfBeds = req.body.typeOfBeds;
const noOfBeds = Number(req.body.noOfBeds);
const sleepers = Number(req.body.sleepers);
const view = req.body.view;
const area = Number(req.body.area);
const price = Number(req.body.price);
const newRoom = new HotelRoom({
name,
numberofRooms,
typeOfBeds,
noOfBeds,
sleepers,
view,
area,
price,
hotelId
})
newRoom.save().then(()=>{
res.json("New Room Added")
}).catch((err)=>{
console.log(err);
})
})
//// here is the postman values
http://localhost:8070/hotel/6432d331380ef51c5e992af1/addroom
{
"name": "Deluxe Suite",
"numberofRooms": 10,
"typeOfBeds": "King",
"noOfBeds": 2,
"sleepers": 2,
"view": "Ocean View",
"area": 60,
"price": 200
}
Here is what saved on mongoDB
_id : ObjectId(‘643662e1e5b1f15e6720c1be’)
name : "Deluxe Suite"
numberofRooms : 10
typeOfBeds : "King"
noOfBeds : 2
sleepers : 2
view : "Ocean View"
area : 60
price : 200
__v
0
I need get hotelId to the database
4
Answers
found the answer
let hotel = req.params.id;
need to be changed toYour field in the
HotelRoomSchema
is calledhotel
nothotelId
. So you need to construct it like so:Try to change
hotelId
intohotel
HotelRoom field name is hotel..But you save field name is hotelId..Please change to hotelId to hotel.
const newRoom = new HotelRoom({