Wie kann dieser Fehler behoben werden? Das Folgende ist das HotelRoom-Modul
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;
/////////Das ist die 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); }) })
//// Das ist der Wert von Postman http://localhost:8070/hotel/6432d331380ef51c5e992af1/addroom
{ "name": "豪华套房", "numberofRooms": 10, "typeOfBeds": "大床", "noOfBeds": 2, "sleepers": 2, "view": "海景", "area": 60, "price": 200 }
Das ist, was auf MongoDB gespeichert ist _id: ObjectId('643662e1e5b1f15e6720c1be') Name: „Deluxe Suite“ Anzahl der Zimmer: 10 typeOfBeds: „Queensize-Bett“ Anzahl der Betten: 2 Schlafplätze: 2 Aussicht: „Meerblick“ Fläche: 60 Preis: 200 __v 0
Ich muss die Hotel-ID in der Datenbank speichern
找到答案
let hotel = req.params.id;
需要修改为