Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 764

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 764

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 580

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/tutopal.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Fix problem of the throw new typeerror('router.use() requires a middleware function but got a ' + gettype(fn)) - TutoPal

Fix problem of the throw new typeerror(‘router.use() requires a middleware function but got a ‘ + gettype(fn))

When working with JavaScript programming language to develop interactive designs, you may encounter a lot of errors, which is obviously normal. Being programmers when you involve in programming using JavaScript, you are working on a long code along with using various libraries and functions to execute the code, but then you suddenly encounter yourself trapped in an error warning. Let’s say you encounter the error ‘problem of the throw new typeerror(‘router.use() requires a middleware function but got a ‘ + gettype(fn))’.

Today we are going to discuss this error in detail to help you get rid of this error warning. So, don’t worry, and go ahead with the post to understand how the error occurs and what measures you need to take to remove the error.

How the error shows up

You got the error warning like this,

throw new TypeError('Router.use() requires middleware function but got a
  ^
 TypeError: Router.use() requires middleware function but got a Object

When you are trying a JavaScript program. Have a look at the code

/Users/datis/Documents/bb-dashboard/node_modules/express/lib/router/index.js:438
  throw new TypeError('Router.use() requires middleware function but got a
  ^
 TypeError: Router.use() requires middleware function but got a Object
  at /Users/datis/Documents/bb-dashboard/node_modules/express/lib/router/index.js:438:13
  at Array.forEach (native)
  at Function.use (/Users/datis/Documents/bb-dashboard/node_modules/express/lib/router/index.js:436:13)
  at /Users/datis/Documents/bb-dashboard/node_modules/express/lib/application.js:188:21
  at Array.forEach (native)
  at Function.use (/Users/datis/Documents/bb-dashboard/node_modules/express/lib/application.js:185:7)
  at Object.<anonymous> (/Users/datis/Documents/bb-dashboard/app.js:46:5)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)

At the time of searching, you may have seen some commands added to the code. But that can’t be the best one because you land in trouble because of that.

Have a look at the app.js code

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('express-session');
var MongoClient = require('mongodb').MongoClient;
var routes = require('./routes/index');
var users = require('./routes/users');

var Users = require('./models/user');
var Items = require('./models/item');
var Store = require('./models/store');
var StoreItem = require('./models/storeitem');

var app = express();
//set mongo db connection
var db = mongoose.connection; 

MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
  if(!err) {
    console.log("We are connected");
  }
});
// var MONGOHQ_URL="mongodb://localhost:27017/test" 

// view engine setup
app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
    secret: 'something',
    resave: true,
    saveUninitialized: true
}));

app.use('/', routes);
app.use('/users', users);
app.use(express.static(path.join(__dirname, 'public')));

// catch 404 and forward to error handler
// app.use(function(req, res, next) {
//     var err = new Error('Not Found');
//     err.status = 404;
//     next(err);
// });

// Make our db accessible to our router
app.use(function(req, res, next){
  req.db = db;
  next();
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;

That’s how you may have coded the program that results in the error.

Solutions to Fix The Error ‘problem of the throw new typeerror(‘router.use() requires a middleware function but got a ‘ + gettype(fn))’

The error occurs because you are missing a few commands in your code. Let’s have a look

Solution 1 – Add the missing code

You need to check your js code file again to see if you are missing any code. Most of the time, you must encounter the error when you miss the following code

module.exports = router;

Adding this in your program code can resolve the issue.

Solution 2 – Replace the Command

You must be using express, and if yes, then you need to replace the below code

app.use('/', routes);

Replace this code with the below one

app.use(app.router);
routes.initialize(app);

This minor addition in your program code can help you get rid of the error warning that you have been experiencing.   

Conclusion

We shed light on the error warning problem of the throw new typeerror(‘router.use() requires a middleware function but got a ‘ + gettype(fn))’. The solutions to this problem are quite simple. You can simply add a few codes in your file and get the error removed.

I hope you find it helpful!

Reference Source: https://www.interface.pub/thread/5183067.html

https://poopcode.com/typeerror-router-use-requires-a-middleware-function-but-got-a-object-how-to-fix-this-express-node-js-error/

Leave a Reply

Your email address will not be published. Required fields are marked *