-1

I have created a file productcontroler.js where i have placed the function which is runign on routes call

exports.getAllProducts() = (req,res)=>{
  res.status(200).json({message:"Route is working fine"});
}

and here is the route

const express = require("express");
const {getAllProducts} = require("../controller/productController.js")

const router = express.Router();


router.route("/product").get(getAllProducts);

and i am getting this error

exports.getAllProducts() = (req,res)=>{
        ^

TypeError: exports.getAllProducts is not a function

after removing the parantheses from getAllproduct in controller file

hrow new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
      ^

TypeError: Router.use() requires a middleware function but got a Object
    at Function.use (C:\Users\saran\OneDrive\Desktop\Learn React\mern\node_modules\express\lib\router\index.js:464:13)
    at Function.<anonymous> (C:\Users\saran\OneDrive\Desktop\Learn React\mern\node_modules\express\lib\application.js:220:21)      
    at Array.forEach (<anonymous>)
    at Function.use (C:\Users\saran\OneDrive\Desktop\Learn React\mern\node_modules\express\lib\application.js:217:7)
    at Object.<anonymous> (C:\Users\saran\OneDrive\Desktop\Learn React\mern\backend\app.js:13:5)
sarangkkl
  • 773
  • 3
  • 15
  • 4
    You don't need the `()` when assigning the property: `exports.getAllProducts =`, otherwise `exports.getAllProducts()` is treated as a method call – Nick Parsons Jan 28 '22 at 12:28
  • when i removed parantheses the error changes i am pasting the new error – sarangkkl Jan 28 '22 at 12:30
  • 1
    Please spend some time debugging and researching the new error yourself before editing (3 minutes isn't enough time). Your original issue and question are resolved by removing the `()`. You now have a new problem that is unrelated to your original question and needs more code to be shown to properly understand what you're doing wrong. You're most likely not exporting your `router`. Please see: [TypeError: Router.use() requires middleware function but got a Object](https://stackoverflow.com/q/27465850) – Nick Parsons Jan 28 '22 at 12:38

1 Answers1

0

you might be forgetting to export one of your files like your router

if you are using const router = express.Router();

don't forget to export module.exports=router;