How to fix this error? The following is the 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;
/////////This is routing
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); }) })
//// This is the value of Postman http://localhost:8070/hotel/6432d331380ef51c5e992af1/addroom
{ "name": "豪华套房", "numberofRooms": 10, "typeOfBeds": "大床", "noOfBeds": 2, "sleepers": 2, "view": "海景", "area": 60, "price": 200 }
This is the content saved on MongoDB _id : ObjectId('643662e1e5b1f15e6720c1be') name: "Deluxe Suite" Number of rooms: 10 typeOfBeds : "Queen bed" Number of beds: 2 Sleepers : 2 view : "sea view" Area: 60 Price: 200 __v 0
I need to save the hotelId into the database
P粉8059312812023-09-10 10:21:49
Find the answer let hotel = req.params.id;
needs to be modified to
let hotel = req.params.hotelId;