Home > Net >  Error in response while trying to create and add new data in mongodb database
Error in response while trying to create and add new data in mongodb database

Time:06-27

getting the following error while sending a request from postman I am trying to create a hotel but when I send my post request to the server I am getting this response saying 500 internal server error.I am not able to find any error in the code .Can anyone tell me what's the issue. I am sending this request

localhost:8000/api/hotel

       {
"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."

}

this is the model Hotel.js file

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)

This is the route 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

This is the main index.js file

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")
})
}

In the console I'm getting

Connected to backend
mongodb connected
Connected to mongodb

CodePudding user response:

first you must check your req.body data. you can validate your req.body with joi validator

and (or) express-joi-validation.

in mongoose you must generate a model from your schema:

const Kitten = mongoose.model('Kitten', kittySchema);

CodePudding user response:

This is the data which I am sending .. not able to figure out what's missing

{
    "name": "lineage",
    "type": "hotel",
    "city": "lucknow",
    "address": "near balaji road",
    "distance": "200kms",
    "title": "Razzuma Hotel",
    "desc": "Best hotel in the city",
    "cheapestPrice": 100
}

can anyone help me with the error I am still getting this error mentioning for every property that is provided the error is defining its subproperties like

"title": {
    "name" :valiadtion error
     "msg" : path "title" is required 
 }

"distance": {
            "name": "ValidatorError",
            "message": "Path `distance` is required.",

}

not able to understand what's the error is all about

  • Related