mongoose 为什么酒店的Object _id没有保存在mongoDB中

hgtggwj0  于 2023-04-21  发布在  Go
关注(0)|答案(4)|浏览(143)

如何修复此错误?这里是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输入数据库

7xzttuei

7xzttuei1#

发现答案let hotel = req.params.id;需要更改为

let hotel = req.params.hotelId;
vulvrdjw

vulvrdjw2#

你在HotelRoomSchema中的字段名为hotel而不是hotelId。所以你需要像这样构造它:

new HotelRoom({ hotel: hotelId, name, numberOfRooms, ... })
ep6jt1vc

ep6jt1vc3#

尝试将hotelId更改为hotel

router.route("/:hotelId/addroom").post((req,res) =>{

    let hotel = 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,
        hotel
    })

    newRoom.save().then(()=>{
        res.json("New Room Added")
    }).catch((err)=>{
        console.log(err);
    })
})
bgtovc5b

bgtovc5b4#

HotelRoom字段名为hotel..但您保存的字段名为hotelId..请将hotelId更改为hotel。
const newRoom = new HotelRoom({

name,
    numberofRooms,
    typeOfBeds,
    noOfBeds,
    sleepers,
    view,
    area,
    price,
    hotel : hotelId
})

相关问题