如何修复此错误?这里是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;
///////这是路线
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);
})
})
////这里是 Postman 值http://localhost:8070/hotel/6432 d331380 ef 51 c5 e992 af 1/addroom
{
"name": "Deluxe Suite",
"numberofRooms": 10,
"typeOfBeds": "King",
"noOfBeds": 2,
"sleepers": 2,
"view": "Ocean View",
"area": 60,
"price": 200
}
以下是保存在mongoDB _id上的内容:ObjectId('643662 e1 e5 b1 f15 e6720 c1 be')名称:“豪华套房”房间数:10种床型:“King”noOfBeds:2根轨枕:②景观:“海景”区:60价格:200 __v 0
我需要把hotelId输入数据库
4条答案
按热度按时间7xzttuei1#
发现答案
let hotel = req.params.id;
需要更改为vulvrdjw2#
你在
HotelRoomSchema
中的字段名为hotel
而不是hotelId
。所以你需要像这样构造它:ep6jt1vc3#
尝试将
hotelId
更改为hotel
bgtovc5b4#
HotelRoom字段名为hotel..但您保存的字段名为hotelId..请将hotelId更改为hotel。
const newRoom = new HotelRoom({