Home > database >  Getting error <pre>Cannot POST /api/category/create/62545ea650265cda5b08f10d</pre>
Getting error <pre>Cannot POST /api/category/create/62545ea650265cda5b08f10d</pre>

Time:04-12

I am learning MERN development, and trying to create API for adding category by using postman. But getting below error

Cannot POST /api/category/create/62545ea650265cda5b08f10d

I am adding here code and screen shot of postman, I would appreciate for any help!

controller\category.js


    const Category = require("../models/category");
    //const { errorHandler } = require("../helpers/dbErrorHandler");
    
    exports.create = (req, res) => {
        const category = new Category(req.body);
        category.save((err, data) => {
            if (err) {
                return res.status(400).json({
                    // error: errorHandler(err)
    
                });
            }
            res.json({ data });
        })
    }

model\category.js


    const mongoose = require('mongoose');
    
    
    const categorySchema = new mongoose.Schema({
        name: {
            type: String,
            trim: true,
            required: true,
            maxlength: 32
        }
    }, { timestamps: true }
    );
    
    
    module.exports = mongoose.model("Category", categorySchema);

routes/category.js


    const express = require('express');
    const router = express.Router();
    
    
    const { create } = require('../controllers/category');
    const { requireSignin, isAuth, isAdmin } = require('../controllers/auth');
    const { userById } = require('../controllers/user');
    
    router.post("/category/create:userId", requireSignin, isAuth, isAdmin, create);
    router.param('userId', userById);
    
    
    module.exports = router; 

  

app.js


    // const express = require('express')
    // const app = express()
    // require('dotenv').config()
    
    // app.get('/',(req, res)=>{
    //     res.send('hello from node updated');
    // })
    
    // const port = process.env.PORT || 8000 
    
    // app.listen(port, ()=>{
    //     console.log(`Servver is running on port ${port}`)
    // });
    
    const express = require('express')
    
    // import mongoose
    const mongoose = require('mongoose');
    const morgan = require('morgan');
    const bodyParser = require('body-parser');
    const cookieParser = require('cookie-parser');
    const expressValidator = require('express-validator');
    // load env variables
    const dotenv = require('dotenv');
    dotenv.config();
    
    //import routes
    const authRoutes = require('./routes/auth');
    const userRoutes = require('./routes/user');
    const categoryRoutes = require('./routes/category');
    
    //app
    const app = express();
    
    //db connection
    mongoose.connect(
      process.env.DATABASE,
      {
        useNewUrlParser: true
      }
    )
      .then(() => console.log('DB Connected'));
    
    mongoose.connection.on('error', err => {
      console.log(`DB connection error: ${err.message}`)
    });
    
    //middlewares
    app.use(morgan('dev'))
    app.use(bodyParser.json())
    app.use(cookieParser());
    app.use(expressValidator());
    
    //routes middleware
    app.use("/api", authRoutes);
    app.use("/api", userRoutes);
    app.use("/api", categoryRoutes);
    
    // app.get('/',(req, res)=>{
    //     res.send('hello from node updated');
    // })
    
    const port = process.env.PORT || 8000
    
    app.listen(port, () => {
      console.log(`Servver is running on port ${port}`)
    });

  [1]: https://i.stack.imgur.com/vKqBt.png

CodePudding user response:

just simply use category/create/:userId in your routes

  • Related