SANTI SINGHA
SANTI SINGHA

Reputation: 21

Error in mongodb " Route.post() requires a callback function but got a [object Undefined]"

This is my main file index.js

type here
const express = require("express");
const app = express();

require("dotenv").config();
const port = process.env.port || 4000;

app.use(express.json());

const createBlog = require("./routes/blogRoutes");

app.use("/santi/api/v1",createBlog);

app.listen(port, () => {
    console.log("App run in 3000 port");
})

const dbConnect = require("./config/database");
dbConnect();

app.get("/", (req,res) => {
    res.send(`<h1>this is homepage</h1>`);
})

this is my routes

const express = require("express");
const router = express.Router();

const {createBlog} = require("../controlers/createBlog");

router.post("/createBlog",createBlog);

module.exports = router;

this is my controller file

const createPost = require("../models/post");

exports.post = async (req,res) => {
    try {
        const {title,description} = req.body;
        const response = await createPost.create({title,description});

        res.status(200).json({
            status:true,
            message:"Insert seccessfully",
            data:response
        })
    }
    catch(error) {
        console.error(err);
        res.status(500).json({
            status:false,
            message:error.message,
            data:"Server issue"
        })
    }
}

this is my model file

const mongoose = require("mongoose");

const blogPostSchema = new mongoose.Schema(
    {
        title:{
            type:String,
            required:true,
            maxlength:50
        },
        description:{
            type:String,
            required:true,
            maxlength:50
        }
    }
)

module.exports = mongoose.model("blogPost",blogPostSchema);

I install nodemon and mongoose but still follwing error show in terminal after write "npm run dev" command

[email protected] dev nodemon index.js

[nodemon] 2.0.22 [nodemon] to restart at any time, enter rs [nodemon] watching path(s): . [nodemon] watching extensions: js,mjs,json [nodemon] starting node index.js C:\Users\SWETTA\OneDrive\Desktop\blog_backend\node_modules\express\lib\router\route.js:211 throw new Error(msg); ^

Error: Route.post() requires a callback function but got a [object Undefined] at Route. [as post] (C:\Users\SWETTA\OneDrive\Desktop\blog_backend\node_modules\express\lib\router\route.js:211:15) at proto. [as post] (C:\Users\SWETTA\OneDrive\Desktop\blog_backend\node_modules\express\lib\router\index.js:521:19) at Object. (C:\Users\SWETTA\OneDrive\Desktop\blog_backend\routes\blogRoutes.js:6:8) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Module.require (node:internal/modules/cjs/loader:1141:19) at require (node:internal/modules/cjs/helpers:110:18) at Object. (C:\Users\SWETTA\OneDrive\Desktop\blog_backend\index.js:9:20)

what is the solution ?

Upvotes: 0

Views: 113

Answers (3)

Hamzahbay
Hamzahbay

Reputation: 11

The error message suggests that the issue is with the post method in your controller file. Specifically, it seems like createBlog in your routes/blogRoutes.js file is not correctly importing the controller function createBlog from your controllers/createBlog.js file.

In your controllers/createBlog.js file, the function is exported as exports.post. However, in your routes/blogRoutes.js file, you are importing it as const {createBlog} = require("../controlers/createBlog");. The key here is that you are importing createBlog, but the actual function in your controller file is called post.

Upvotes: 0

Younes Bougrine
Younes Bougrine

Reputation: 1

the issue is in your controller file, you are exporting the function under the name post and importing it in your routes file as createBlog. To correct your code you should update your controller and rename the function as createBlog :

const createPost = require("../models/post");

exports.createBlog = async (req,res) => {
    try {
        const {title,description} = req.body;
        const response = await createPost.create({title,description});

        res.status(200).json({
            status:true,
            message:"Insert seccessfully",
            data:response
        })
    }
    catch(error) {
        console.error(err);
        res.status(500).json({
            status:false,
            message:error.message,
            data:"Server issue"
        })
    }
}

Upvotes: 0

sachin
sachin

Reputation: 1117

I see a small error in your router code.
You are exporting the function as post in your controller and importing it in router as createBlog. And your controller does not export anything with the name createBlog and hence the issue is.

Just change the router code to this:

const express = require("express");
const router = express.Router();

const {post} = require("../controlers/createBlog");

router.post("/createBlog",post);

module.exports = router;

Upvotes: 0

Related Questions