Rumah > Soal Jawab > teks badan
Bagaimana untuk membetulkan ralat ini? Berikut ialah modul HotelRoom
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;
/////////Inilah laluannya
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); }) })
//// Inilah nilai Posmen http://localhost:8070/hotel/6432d331380ef51c5e992af1/addroom
{ "name": "豪华套房", "numberofRooms": 10, "typeOfBeds": "大床", "noOfBeds": 2, "sleepers": 2, "view": "海景", "area": 60, "price": 200 }
Inilah yang disimpan pada MongoDB _id : ObjectId('643662e1e5b1f15e6720c1be') nama: "Suite Deluxe" Bilangan bilik: 10 typeOfBeds : "Katil queen" Bilangan katil: 2 Tempat tidur: 2 pandangan : "Pemandangan laut" Keluasan: 60 Harga: 200 __v 0
Saya perlu menyimpan hotelId ke pangkalan data
P粉8059312812023-09-10 10:21:49
Mendapat jawapan let hotel = req.params.id;
perlu diubah suai kepada
let hotel = req.params.hotelId;