The module send the empty data in the database can not retrive the body value from the request of body?

Viewed 42

Here is my code whic is used to send the name of category and its slug in the database but its send the empty value only the id in the database I think it cannot retrive the name and slug name frome the body of request.aur maybe its retrive it aur doest not trnafer its json

const express = require('express');
const router=express.Router();
const { body, validationResult } = require('express-validator');


const Category =require('../models/Category');
//route1:add a category using post "/api/authent/addcategory".  login req
 router.post('/addcategory',[
    body('name', 'you enter wrong name').isLength({ min: 2 }),
    body('slug', 'Please enter at least 2 characters').isLength({ min: 2 }),

 ],async(req,res)=>{
   
    try {
        const { name, slug } = req.body;
        
    //if there are errors ,return bad request and the error
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(400).json({ errors: errors.array() });
    }
    const cat = new Category({
        name,slug
    })
    const savecat=await cat.save();
    res.json(savecat);
    } catch (error) {
           
    }






 });
 module.exports=router;

Here is the models of category where I created the name and the slug of category I added it for your assistense.

const mongoose = require('mongoose');
    const { schema } = require('./User');
    const { Schema } = mongoose;
    
    const CategorySchema=new Schema({
    
    name:{
        type:String,
        required:true,
        trim:true
    },
    slug:{
        type:String,
        required:true,
        unique:true
    },
    parentId:{
        type:String
    }
    
    
    
    
    
    
    });
    const Category=mongoose.model('Category',CategorySchema);
    module.exports=Category;

Here is my index.js

const connectToMongo=require('./db');
var cors = require('cors');
const express = require('express')

connectToMongo();
const app = express()
app.use(cors())
const port = 5000


app.use(express.json());
app.use('/api/authent',require('./routes/authent'))
app.use('/api/Categories',require('./routes/Categories'))


app.listen(port, () => {
  console.log(`iNoteBook Backend listening on port ${port}`)
})

This image show the body of my request

The header I added it content type:Json application

0 Answers
Related