Home > Software design >  requires a callback function but got a [object Undefined]
requires a callback function but got a [object Undefined]

Time:10-26

I am creating a RESTful API using Node.js , express and My sql. I have created 2 Services,2Routers, and 2 Controllers

App.js module

require("dotenv").config();
const express = require("express");
const app = express();
const userRouter = require("./api/Routers/user.router");
const categoryRouter=require("./api/Routers/category.router");


app.use(express.json());



app.use("/api/users", userRouter);
app.use("/api/category", categoryRouter);
const port = process.env.PORT ;
app.listen(port, () => {
  console.log("server up and running on PORT :", port);
});

Category.service.js

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

module.exports = {
  create: (data, callBack) => {
    pool.query(
      `insert into category(name, , branch, state, create_date, modified_date) 
                values(?,?,?,?,?,?)`,
      [
        data.name,
        data.branch,
        data.state,
        data.create_date,
        data.modified_date,
       
      ],
      (error, results, fields) => {
        if (error) {
          callBack(error);
        }
        return callBack(null, results);
      }
    );
  },
  getUserByUserEmail: (email, callBack) => {
    pool.query(
      `select * from registration where email = ?`,
      [email],
      (error, results, fields) => {
        if (error) {
          callBack(error);
        }
        return callBack(null, results[0]);
      }
    );
  },
  getCategoryByCategoryId: (id, callBack) => {
    pool.query(
      `select id,name,branch,state,create_date,modified_date from category where id = ?`,
      [id],
      (error, results, fields) => {
        if (error) {
          callBack(error);
        }
        return callBack(null, results[0]);
      }
    );
  },
  getCategory: callBack => {
    pool.query(
      `select id,name,branch,state,create_date,modified_date  from category`,
      [],
      (error, results, fields) => {
        if (error) {
          callBack(error);
        }
        return callBack(null, results);
      }
    );
  },
  updateCategory: (data, callBack) => {
    pool.query(
      `update category set name=?, branch=?, state=?, create_date=?, modified_date=?, `,
      [
        data.name,
        data.branch,
        data.state,
        data.create_date,
        data.modified_date,
       
      ],
      (error, results, fields) => {
        if (error) {
          callBack(error);
        }
        return callBack(null, results[0]);
      }
    );
  },
  deleteCategory: (data, callBack) => {
    pool.query(
      `delete from category where id = ?`,
      [data.id],
      (error, results, fields) => {
        if (error) {
          callBack(error);
        }
        return callBack(null, results[0]);
      }
    );
  }
};

Category.route.js

const router = require("express").Router();
const { checkToken } = require("../../auth/token_validation");
const {
  createCategory,
  login,
  getCreateByCreateId,
  getCategory,
  updateCategory,
  deleteCategory
} = require("../Controllers/category.controller");
router.get("/", checkToken, getCategory);
router.post("/", checkToken, createCategory);
router.get("/:id", checkToken,  getCreateByCreateId);
router.post("/login", login);
router.patch("/", checkToken, updateCategory);
router.delete("/", checkToken, deleteCategory);

module.exports = router;


Category.controll.js

const {
  create,
  getUserByUserEmail,
  getCategoryByCategoryId,
  getCategory,
  updateCategory,
  deleteCategory
} = require("../services/category.service");
const { hashSync, genSaltSync, compareSync } = require("bcrypt");
const { sign } = require("jsonwebtoken");

module.exports = {
  createUser: (req, res) => {
    const body = req.body;
    const salt = genSaltSync(10);
    body.password = hashSync(body.password, salt);
    create(body, (err, results) => {
      if (err) {
        console.log(err);
        return res.status(500).json({
          success: 0,
          message: "Database connection error"
        });
      }
      return res.status(200).json({
        success: 1,
        data: results
      });
    });
  },
  login: (req, res) => {
    const body = req.body;
    getUserByUserEmail(body.email, (err, results) => {
      if (err) {
        console.log(err);
      }
      if (!results) {
        return res.json({
          success: 0,
          data: "Invalid email or password"
        });
      }
      const result = compareSync(body.password, results.password);
      if (result) {
        results.password = undefined;
        const jsontoken = sign({ result: results }, process.env.JWT_KEY, {
          expiresIn: "1h"
        });
        return res.json({
          success: 1,
          message: "login successfully",
          token: jsontoken
        });
      } else {
        return res.json({
          success: 0,
          data: "Invalid email or password"
        });
      }
    });
  },
  getCategoryByCategoryId: (req, res) => {
    const id = req.params.id;
    getCategoryByCategoryId(id, (err, results) => {
      if (err) {
        console.log(err);
        return;
      }
      if (!results) {
        return res.json({
          success: 0,
          message: "Record not Found"
        });
      }
      results.password = undefined;
      return res.json({
        success: 1,
        data: results
      });
    });
  },
  getCategory: (req, res) => {
    getCategory((err, results) => {
      if (err) {
        console.log(err);
        return;
      }
      return res.json({
        success: 1,
        data: results
      });
    });
  },
  updateCategory: (req, res) => {
    const body = req.body;
    const salt = genSaltSync(10);
    body.password = hashSync(body.password, salt);
    updateCategory(body, (err, results) => {
      if (err) {
        console.log(err);
        return;
      }
      return res.json({
        success: 1,
        message: "updated successfully"
      });
    });
  },
  deleteCategory: (req, res) => {
    const data = req.body;
    deleteCategory(data, (err, results) => {
      console.log(results);
      if (err) {
        return;
      }
      if (!results) {
        return res.json({
          success: 0,
          message: "Record Not Found",
          h:results
        });
      }
      return res.json({
        success: 1,
        message: "category deleted not successful"
      });
    });
  }
};

I am getting Error: Route.post() requires a callback function but got a [object Undefined. how can i sort it out

Error: Route.post() requires a callback function but got a [object Undefined] how can I sort this error

CodePudding user response:

createUser should be renamed in createCategory in Category.controll.js

  • Related