Database Query In Express Error: Router.use() Requires A Middleware Function But Got A Undefined

- 1 answer

I'm setting up a controller, model, route, and view in my express application. Modifying my application that was working with just a view, I am getting this error: Router.use() requires a middleware function but got a undefined

I got it to work with just the pug view and server.js files, but once I added the model/controller/routes, its throwing this error.

# server.js
// http://localhost:8099/tasks
var router = express.Router();
var tasksRouter = require('./routes/tasks');
app.use('/tasks', tasksRouter.tasks_list);
# models\tasks.js
const mysql = require('mysql');

// connection configurations
const mc = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'mysql',
    port: '2001'

// connect to database

module.exports = mc;
# controllers\tasksController.js
var Tasks = require('../models/tasks');

// Display list of all tasks.
exports.tasks_list = function(req, res, next) {

    mc.query('SELECT * FROM tasks', function (error, results, fields) {
     if (error) throw error;
     //console.log('The solution is: ', results);
      res.render('tasks_list', {
        title: 'Results',
        data: results

var express = require('express');
var router = express.Router();

// Require controller modules.
var tasks_controller = require('../controllers/tasksController');

/// ROUTES ///

// GET request for list of all tasks.
router.get('/tasks', tasks_controller.tasks_list);

module.exports = router;
extends layout

block content
  h1= title
  p Welcome to #{title}
      for tasks in data
          td #{}
          td #{tasks.task}
          td #{tasks.status}
          td.dt-body-right.sorting_1 #{tasks.created_at}

I should be shown a page in the browser of the table data parsed into html. But in my console I'm getting: c:\ExpressProject4\express-node-rest-project\node_modules\express\lib\router\index.js:458 throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) ^

TypeError: Router.use() requires a middleware function but got a undefined at Function.use (c:\ExpressProject4\express-node-rest-project\node_modules\express\lib\router\index.js:458:13) at Function. (c:\ExpressProject4\express-node-rest-project\node_modules\express\lib\application.js:220:21) at Array.forEach () at Function.use (c:\ExpressProject4\express-node-rest-project\node_modules\express\lib\application.js:217:7) at Object. (c:\ExpressProject4\express-node-rest-project\server.js:34:5) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3)



The subproperty tasks_list you are referencing here:

app.use('/tasks', tasksRouter.tasks_list);

Is not assigned in tasks/router.js - you need to either restructure the exported object or change the way it is reference when require-d.