Mongo url shorter TypeError: db.collection is not a function

var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var path = require('path');

var app = express();

var urlDB = 'mongodb://localhost:27017/data';

//connect to db
MongoClient.connect(urlDB, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error: ', err);
  } else {
    console.log('Connection established to ', urlDB);
  }
  var port = process.env.PORT || 3500;

  app.listen(port, function(){
    console.log("Listening on port: " + port);
  });

  app.get('/new/:url(*)', function (req, res) {
    var url = req.params.url;
    var id = Math.floor(Math.random() * 10000);
    var retObj = {  "original_url": url ,
                    "short_url": "https://little-url.herokuapp.com/" + id
                  };
    db.collection("data").insert(retObj, function(err, data) {
      if (err) {console.log("Insert db err: " + err);}
    });
    res.json(retObj);
  });
});

When I go to http://localhost:3500/new/wowz I just get “TypeError: db.collection is not a function”

Thanks, unfortunately that’s not the issue. Even if I try to put db.collection(“data”).insert({“my”:obj}); right after the if/else I still get the same error of “TypeError: db.collection is not a function”

Thanks for getting back to me, console.log(db, typeof db) shows
‘object’

MongoClient {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
s:
{ url: ‘mongodb://localhost:27017/data’,
options:
{ socketOptions: {},
read_preference_tags: null,
readPreference: [Object],
dbName: ‘data’,
servers: [Object],
server_options: [Object],
db_options: [Object],
rs_options: [Object],
mongos_options: [Object],
socketTimeoutMS: 360000,
connectTimeoutMS: 30000,
promiseLibrary: [Function: Promise] },
promiseLibrary: [Function: Promise],
dbCache: {},
sessions: [] },
topology:
Server {
domain: null,
_events:
{ serverOpening: [Function],
serverDescriptionChanged: [Function],
serverHeartbeatStarted: [Function],
serverHeartbeatSucceeded: [Function],
serverHeartbeatFailed: [Function],
serverClosed: [Function],
topologyOpening: [Function],
topologyClosed: [Function],
topologyDescriptionChanged: [Function],
joined: [Function],
left: [Function],
ping: [Function],
ha: [Function],
authenticated: [Function],
error: [Function],
timeout: [Function],
close: [Function],
parseError: [Function],
open: [Object],
fullsetup: [Object],
all: [Object],
reconnect: [Function] },
_eventsCount: 22,
_maxListeners: undefined,
clientInfo:
{ driver: [Object],
os: [Object],
platform: ‘Node.js v6.10.3, LE’ },
s:
{ coreTopology: [Object],
sCapabilities: null,
clonedOptions: [Object],
reconnect: true,
emitError: true,
poolSize: 5,
storeOptions: [Object],
store: [Object],
host: ‘localhost’,
port: 27017,
options: [Object],
sessionPool: [Object],
promiseLibrary: [Function: Promise] } } }

I fount the solution here https://stackoverflow.com/questions/47662220/db-collection-is-not-a-function-when-using-mongoclient-v3-0

Just use this for mongo 3.^

MongoClient.connect('mongodb://localhost:27017', (err, client) => {
  // Client returned
  var db = client.db('mytestingdb');
});
5 Likes