URL Shortener Microservice, db.collection is not a function [SOLVED]

URL Shortener Microservice, db.collection is not a function [SOLVED]
0

Don’t understand what I’m doing wrong.

Server.js

'use strict';

var express = require('express');
var mongodb = require('mongodb');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cors = require('cors');
var assert = require('assert');

var mongoClient = mongodb.MongoClient;
var dbUrl = process.env.MONGOLAB_URI;

var app = express();

// Basic Configuration 
var port = process.env.PORT || 3000;

/** this project needs a db !! **/ 
mongoose.connect(process.env.MONGOLAB_URI);

app.use(cors());

/** this project needs to parse POST bodies **/
// you should mount the body-parser here

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use('/public', express.static(process.cwd() + '/public'));

app.get('/', function(req, res) {
  res.sendFile(process.cwd() + '/views/index.html');
});
  
// your first API endpoint... 

app.get('/favicon.ico', function(req, res) {
  res.status(204);
});

mongoClient.connect(dbUrl, (err, client) => {
  
  const db = client.db;

  if (err) { console.log('Unable to connect to the mongoDB server. Error:', err);
  
  } else {

    console.log('Connection established to', dbUrl);

    app.get("/api/shorturl/new/:url(*)", function(req, res) {
      
      var request = req.params.url;

      if (/^https?:\/\//.test(request) === true) {

        var collection = db.collection('tinyUrls');

        var tinyUrl = {};
        
        collection.count(function(err, result) {
          
          if (err) { console.log(err);

          } else {

            var num = result + 1;

            var shortUrl = process.env.APP_URL + num;

            tinyUrl = {index: num, original_url: request, short_url: shortUrl};

            collection.insert(tinyUrl, function(err, result) {

              if (err) { console.log(err);

              } else { console.log('Inserted %d documents into the tinyUrls collection. The documents inserted with "_id" are:', result.length, result);}
            
            });
            
            res.json({'original_url': request, 'short_url': shortUrl});
          }
        }); 
        
      } else { res.json({'error': 'invalid URL'});}
      
    }); 

    app.get('/api/shorturl/:index', function(req,res) {

      var urlNumber = req.params.index;

      var collection = db.collection('tinyUrls');        

      var query = {};

      var index = 'index';

      query.index = parseInt(urlNumber);

      collection.findOne(query, function(err, document) {

        if (err) { console.log(err); };

        if (document) { res.redirect(document.original_url);

        } else { res.json({'error': 'invalid URL'});}

      });
    
    }); 
  
  }

}); 

app.listen(port, function() {
  console.log('Node.js listening ...');
});
  

error

TypeError: db.collection is not a function
    at /app/server.js:57:29
    at Layer.handle [as handle_request] (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/layer.js:95:5)
    at next (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/layer.js:95:5)
    at /rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/index.js:281:22
    at param (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/index.js:354:14)
    at param (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/index.js:365:14)
    at param (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/index.js:410:3)
    at next (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/index.js:275:10)
    at jsonParser (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/body-parser/1.19.0/node_modules/body-parser/lib/types/json.js:110:7)
    at Layer.handle [as handle_request] (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/index.js:317:13)
    at /rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/rbd/pnpm-volume/70e5050c-8545-4b18-b37d-8be1dfb14c54/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/router/index.js:335:12)

As of mongoDB v3 and up, replace

mongoClient.connect(dbUrl, (err, client) => {
  
  var db = client.db;

to

mongoClient.connect(dbUrl, (err, client) => {

var db = client.db('shortUrlsDB');