从邮递员发送请求时收到以下错误我试图创建一个酒店,但当我发送我的帖子请求到服务器,我得到这个响应说500内部服务器错误。我在代码中找不到任何错误,谁能告诉我是什么问题?我正在发送这个请求
localhost: 8000/api/酒店
{
"errors": {
"cheapestPrice": {
"name": "ValidatorError",
"message": "Path `cheapestPrice` is required.",
"properties": {
"message": "Path `cheapestPrice` is required.",
"type": "required",
"path": "cheapestPrice"
},
"kind": "required",
"path": "cheapestPrice"
},
"desc": {
"name": "ValidatorError",
"message": "Path `desc` is required.",
"properties": {
"message": "Path `desc` is required.",
"type": "required",
"path": "desc"
},
"kind": "required",
"path": "desc"
},
"title": {
"name": "ValidatorError",
"message": "Path `title` is required.",
"properties": {
"message": "Path `title` is required.",
"type": "required",
"path": "title"
},
"kind": "required",
"path": "title"
},
"distance": {
"name": "ValidatorError",
"message": "Path `distance` is required.",
"properties": {
"message": "Path `distance` is required.",
"type": "required",
"path": "distance"
},
"kind": "required",
"path": "distance"
},
"address": {
"name": "ValidatorError",
"message": "Path `address` is required.",
"properties": {
"message": "Path `address` is required.",
"type": "required",
"path": "address"
},
"kind": "required",
"path": "address"
},
"city": {
"name": "ValidatorError",
"message": "Path `city` is required.",
"properties": {
"message": "Path `city` is required.",
"type": "required",
"path": "city"
},
"kind": "required",
"path": "city"
},
"type": {
"name": "ValidatorError",
"message": "Path `type` is required.",
"properties": {
"message": "Path `type` is required.",
"type": "required",
"path": "type"
},
"kind": "required",
"path": "type"
},
"name": {
"name": "ValidatorError",
"message": "Path `name` is required.",
"properties": {
"message": "Path `name` is required.",
"type": "required",
"path": "name"
},
"kind": "required",
"path": "name"
}
},
"_message": "Hotel validation failed",
"name": "ValidationError",
"message": "Hotel validation failed: cheapestPrice: Path `cheapestPrice` is required., desc: Path `desc` is required., title: Path `title` is required., distance: Path `distance` is required., address: Path `address` is required., city: Path `city` is required., type: Path `type` is required., name: Path `name` is required."
}
这是模型Hotel.js文件
import mongoose from "mongoose";
const {Schema} = mongoose
const HotelSchema = new mongoose.Schema({
name:{
type:String,
required:true
},
type:{
type:String,
required:true
},
city:{
type:String,
required:true
},
address:{
type:String,
required:true
},
distance:{
type:String,
required:true
},
photos:{
type:[String],
},
title:{
type:String,
required:true
},
desc:{
type: String,
required:true
},
rating:{
type: Number,
min:0,
max:5
},
rooms:{
type:[String]
},
// for showing cheapest hotels
cheapestPrice:{
type:Number,
required:true
},
// for showing featured hotels
featured:{
type:Boolean,
deafult:false,
}
})
export default mongoose.model("Hotel",HotelSchema)
这是hotel.js的路由
import express from "express"
import Hotel from "../models/Hotel.js";
const router = express.Router();
router.post("/", async (req,res)=>{
const newHotel = new Hotel(req.body);
try{
const savedHotel = await newHotel.save()
res.status(200).json(savedHotel)
}catch(err){
res.status(500).json(err)
}
})
export default router
这是主index.js文件
import express from "express"
import dotenv from "dotenv"
import mongoose from "mongoose"
import hotelRoute from './routes/hotels.js'
const app = express()
dotenv.config()
const connect = async () =>{
try{
await mongoose.connect(process.env.MONGO)
console.log("Connected to mongodb")
}catch(err){
throw err;
}
}
mongoose.connection.on("connected",()=>{
console.log("mongodb connected")
})
mongoose.connection.on("disconnected",()=>{
console.log("mongodb disconnected")
})
//Middleware
app.use(express.urlencoded({extended:true}))
app.use(express.json())
app.use("/api/hotel",hotelRoute)
app.listen(8000,() =>{
connect()
console.log("Connected to backend")
})
}
在控制台我得到
Connected to backend
mongodb connected
Connected to mongodb
首先你必须检查你的请求。身体数据。您可以验证您的请求。Body with joi validator
和(或)expression - join -validation.
在mongoose中,你必须从你的模式生成一个模型:
const Kitten = mongoose.model('Kitten', kittySchema);
post方法URL = http://localhost:8800/api/hotels
尝试使用http://
当您通过Postman使用POST方法时。同样的问题也发生在我身上。我缺少的是JSON的数据类型。
您可能将请求作为数据类型文本发送。
在相同的项目中得到了相同的错误,但我通过执行
获得了200状态的结果res.status(200).json(hotels);
在hotels.js
文件中的所有GET,PUT,POST,DELETE方法中代替
req.send(200) *or* req.send(500)
希望这是有用的!!谢谢!