Route.get() requires a callback function but got a [object Undefined] error

Issue

This Content is from Stack Overflow. Question asked by cypherduck

seems like I am stuck with a routing problem in my NodeJS app. I don’t really get where that error comes from. Obviously I am missing a callback function but I don’t understand where and why.. Anyone willing to help? 🙂
Here is my code:

This is my index.js file:

const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
require('dotenv').config()

const app = express();

var corsOptions = {
    origin: "http://localhost:3000"
};
app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));

// connect to database
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error: "));
db.once("open", function () {
    console.log("Connected successfully");
});

// routes
require('./routes/auth.routes')(app);
require('./routes/user.routes')(app);
require('./routes/dog.routes')(app);


app.listen(3001, () => {
    console.log(`Example app listening on port ${3001}`)
})

This is my dog.routes.js file:

const { authJwt } = require("../middleware");
const controller = require("../controllers/dog.controller");

module.exports = function (app) {
    app.use(function (req, res, next) {
        res.header(
            "Access-Control-Allow-Headers",
            "x-access-token, Origin, Content-Type, Accept"
        );
        next();
    });

    app.post("/dog/addDog", [authJwt.verifyToken], controller.addDog);

    app.get("/dog/getDogs", controller.allAcces, controller.getDogs);
}

This is my dog.controller.js file:

const dogModel = require("../models/dog.model");

exports.addDog = async (req, res) => {
    const dog = new dogModel(req.body);
    try {
        await dog.save();
        res.status(200).send(dog);
    } catch (error) {
        res.status(500).send(error);
    }
};

exports.getDogs = async (req, res) => {
    try {
        const dogs = await dogModel.find({});
        res.status(200).send(dogs);
    } catch (error) {
        res.status(500).send(error);
    }
};

An this is the error:

Error: Route.get() requires a callback function but got a [object Undefined]
    at Route.<computed> [as get] (/Users/freddy/Desktop/fellPfoten/backend/node_modules/express/lib/router/route.js:211:15)
    at Function.app.<computed> [as get] (/Users/freddy/Desktop/fellPfoten/backend/node_modules/express/lib/application.js:499:19)
    at module.exports (/Users/freddy/Desktop/fellPfoten/backend/routes/dog.routes.js:15:9)
    at Object.<anonymous> (/Users/freddy/Desktop/fellPfoten/backend/index.js:29:31)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
    at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...

Thank you so much!!



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?