MERN

⌘K
  1. Home
  2. Docs
  3. MERN
  4. Category

Category

models/categoryModel.js

const mongoose = require('mongoose');

const CategorySchema = new mongoose.Schema({
  name: { type: String, required: true, unique: true },
  description: { type: String },
});

const Category = mongoose.model('Category', CategorySchema);

module.exports = Category;

routes/categoryRoute.js

const categoryRoute = require('express').Router();
const Category = require('../models/categoryModel');


// Create a category
categoryRoute.post('/', async (req, res) => {
    try {
      const { name, description } = req.body;
      const newCategory = new Category({ name, description });
      await newCategory.save();
      res.status(201).json({ message: 'Category created successfully' });
    } catch (error) {
      console.error(error);
      res.status(500).json({ error: 'Internal Server Error' });
    }
  });

  
// Get all categories
categoryRoute.get('/', async (req, res) => {
    try {
      const categories = await Category.find();
      res.status(200).json(categories);
    } catch (error) {
      console.error(error);
      res.status(500).json({ error: 'Internal Server Error' });
    }
  });
  
  // Get a single category by ID
  categoryRoute.get('/:id', async (req, res) => {
    try {
      const categoryId = req.params.id;
      const category = await Category.findById(categoryId);
  
      if (!category) {
        return res.status(404).json({ error: 'Category not found' });
      }
  
      res.status(200).json(category);
    } catch (error) {
      console.error(error);
      res.status(500).json({ error: 'Internal Server Error' });
    }
  });
  
  // Update a category by ID
  categoryRoute.put('/:id', async (req, res) => {
    try {
      const categoryId = req.params.id;
      const { name, description } = req.body;
  
      const updatedCategory = await Category.findByIdAndUpdate(
        categoryId,
        { name, description },
        { new: true } // Return the updated document
      );
  
      if (!updatedCategory) {
        return res.status(404).json({ error: 'Category not found' });
      }
  
      res.status(200).json(updatedCategory);
    } catch (error) {
      console.error(error);
      res.status(500).json({ error: 'Internal Server Error' });
    }
  });
  
  // Delete a category by ID
  categoryRoute.delete('/:id', async (req, res) => {
    try {
      const categoryId = req.params.id;
      const deletedCategory = await Category.findByIdAndDelete(categoryId);
  
      if (!deletedCategory) {
        return res.status(404).json({ error: 'Category not found' });
      }
  
      res.status(200).json({ message: 'Category deleted successfully' });
    } catch (error) {
      console.error(error);
      res.status(500).json({ error: 'Internal Server Error' });
    }
  });
module.exports = categoryRoute;

index.js

const categoryRoute = require('./routes/categoryRoute'); // Add this line
app.use('/api/category', categoryRoute); // Add this line

category+Post linked



const PostSchema = new mongoose.Schema({


    category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', required: true },

});

in postman

{
   
    "title":"01953664967",
     "content":"501302aA@",
     "category":"6559dd94a9f6ba2dce0cec14"

}

How can we help?