title: Error connection MongoDB: Error: Route.get() requires a callback function but got a [object Undefined]
body: <p>I have been trying to connect my application to <code>MongoDB</code> using <code>express</code> but without success. Below the most important part of the code:</p>
  
  <p><strong>app.js</strong>:</p>
  
  <pre><code>var createError = require('http-errors');
  var express = require('express');
  var path = require('path');
  var cookieParser = require('cookie-parser');
  var logger = require('morgan');
  var cors = require('cors');
  
  var indexRouter = require('./routes/index');
  var usersRouter = require('./routes/users');
  
  var mongoose = require('mongoose');
  const bodyParser = require('body-parser');
  const vesselController = require('./controllers/VesselController');
  require('./config/keys');
  
  var app = express();
  app.use(cors());
  app.options('*', cors());
  
  // DB Config
  const db = require('./config/keys').MongoURI;
  
  const options = {
      useNewUrlParser: true,
      reconnectTries: Number.MAX_VALUE,
      poolSize: 10
  };
  
  mongoose
      .connect(db, options)
      .then(() =&gt; console.log('MongoDB Connection established'))
      .catch((err) =&gt; console.log('Error connecting MongoDB database due to: ', err));
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  
  // Bodyparser
  app.use(express.urlencoded({ extended: false }));
  
  app.use(logger('dev'));
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use('/', indexRouter);
  app.use('/users', usersRouter);
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
      next(createError(404));
  });
  
  app.use((req, res, next) =&gt; {
      res.header('Access-Control-Allow-Origin', '*');
      next();
  });
  
  // error handler
  app.use(function(err, req, res, next) {
      // set locals, only providing error in development
      res.locals.message = err.message;
      res.locals.error = req.app.get('env') === 'development' ? err : {};
  
      // render the error page
      res.status(err.status || 500);
      res.render('error');
  });
  
  const PORT = process.env.PORT || 3000;
  
  app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
  app.use(bodyParser.json({ limit: '50mb' }));
  app.use(cors());
  
  app.route('/vessels/all').get(vesselController.getBaseAll);
  app.route('vessels/:id/track').get(vesselController.getCurrent);
  app.route('/vessels').get(vesselController.getHistory);
  
  app.listen(PORT, console.log(`Server started on port ${PORT}`));
  
  module.exports = app;
  </code></pre>
  
  <p><strong>VesselController.js</strong></p>
  
  <pre><code>const Vessels = require('../models/Vessels');
  const Positions = require('../models/Positions');
  const Compnanies = require('../models/Companies');
  
  exports.getBaseAll = (req, res) =&gt; {
      Promise.all([
          Compnanies.find(),
          Vessels.find(),
          Positions.aggregate([
              {
                  $sort: {
                      date: -1
                  }
              },
              {
                  $group: {
                      _id: '$callsign',
                      details: {
                          $push: '$$ROOT'
                      }
                  }
              },
              {
                  $replaceRoot: {
                      newRoot: {
                          $arrayElemAt: [ '$details', 0 ]
                      }
                  }
              }
          ])
      ])
          .then(([ companies, vessels, positions ]) =&gt; {
              // apply vessels detail table as join:
              positions.forEach((pos) =&gt; {
                  vessels.forEach((ves) =&gt; {
                      if (pos.callsign == ves.callsign) {
                          p._detail = ves;
                      }
                  });
                  companies.forEach((com) =&gt; {
                      if (p._detail.company == com.number) {
                          p._detail = com;
                      }
                  });
              });
              res.status(200).json(positions);
          })
          .catch((err) =&gt; {
              return res.status(500).send(err);
          });
  };
  
  exports.getHistory = (req, res) =&gt; {
      var id = req.param.id;
      Positions.find(
          {
              callsign: id,
              date: {
                  $gte: new Date(Date.now() - 1000 * 60 * 60 * 24)
              }
          },
          (err, task) =&gt; {
              if (err) {
                  return res.status(500).send(err);
              }
              res.status(200).json(task);
          }
      );
  };
  
  exports.getCurrent = (req, res) =&gt; {
      var currentPos = Positions.find({
          date: {
              $gte: new Date(Date.now() - 1000 * 60 * 60)
          }
      });
      currentPos.exec((err, task) =&gt; {
          if (err) {
              return res.status(500).send(err);
          }
          res.status(200).json(task);
      });
  };
  </code></pre>
  
  <p>I am not sure if the problem is due to the fact that I am trying to connect to a specific database. After hitting <strong>connect</strong> to my application as shown below and copy/paste the key:</p>
  
  <p><img src="https://i.imgur.com/R5KC7zW.png" alt="connection"></p>
  
  <pre><code>mongodb+srv://&lt;username&gt;:&lt;password&gt;@vessel-tracker-cluster-x2lpw.mongodb.net/test?retryWrites=true&amp;w=majority
  </code></pre>
  
  <p>Below is how my cluser is organized:</p>
  
  <p><img src="https://i.imgur.com/qwNvGVT.png" alt="cluster"></p>
  
  <p>And after accessing the collections you can see how the database is structured:</p>
  
  <p><img src="https://i.imgur.com/AuqdohB.png" alt="db"></p>
  
  <p><strong>What I have done</strong> to solve the problem:</p>
  
  <p>Posts that I came across and analyzed to solve the problem were:</p>
  
  <p>1) <a href="https://stackoverflow.com/questions/21124909/express-routes-get-requires-callback-functions-but-got-a-object-object">This</a> was useful but I could not solve the problem.</p>
  
  <p>2) I used <a href="https://stackoverflow.com/questions/42216932/error-route-get-requires-callback-functions-but-got-a-object-undefined">this other source</a> but problem still stays.</p>
  
  <p>3) <a href="https://stackoverflow.com/questions/42775212/nodejs-route-get-requires-callback-functions-but-got-a-object-string">This one</a> was also useful but my problem still stay and <code>MongoDB</code> is not properly connecting</p>
  
  <p>Thanks for pointing in the right direction for solving this problem.</p>