I am trying to post data from POSTMAN to an external database that I created on mLab but I am getting the error db.collection is not a function.

There is a similar question thread but the answer is incomplete and doesn't save any keys/values I put into postman to mLab. The code that I am trying to make work is from this tutorial: <https://medium.freecodecamp.com/building-a-simple-node-js-api-in-under-30-minutes-a07ea9e390d2>

**My Code:** 

Server.js

```
const express  = require('express'); // Load routes application
const MongoClient = require('mongodb').MongoClient; //Load database connection application
const db = require('./config/db');
const app  = express(); // Assign express app a variable
const port = 8000; //Set local port value for server
const bodyParser = require('body-parser'); // **This has to come BEFORE routes
var assert = require('assert'); // ?
var databaseURL ='mongodb://external:api@ds123312.mlab.com:23312/soundfactory';

app.listen(port, () => {
  console.log('')
  console.log('We are live on ' + port);
  console.log('')
});

MongoClient.connect(databaseURL, function(err, db) {
  assert.equal(null, err);
  console.log("API has succesfully connected to Sound Facotry mlab external database.");
  console.log('')
  db.close();
});

app.use(bodyParser.urlencoded({ extended: true }))
require('./app/routes')(app, {}); //Must come AFTER express w/ body parser

```

db.js

```
module.exports = {
  url : 'mongodb://external:api@ds123312.mlab.com:23312/soundfactory'
};

```

index.js

```
const noteroutes = require('./note_routes');
module.exports = function(app,db)
{
    noteroutes(app,db);
};

```

note\_routes.js

```
module.exports = function(app, db) {
  app.post('/notes', (req, res) => {
    const note = { text: req.body.body, title: req.body.title };
    db.collection('notes').insert(note, (err, result) => {
      if (err) { 
        res.send({ 'error': 'An error has occurred' }); 
      } else {
        res.send(result.ops[0]);
      }
    });
  });
};

```

**partially correct code**

server.js (code that partially works & doesn't throw the db.collections error like my original server.js file ) 

```
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const db = require('./config/db');
const app = express();
const port = 8000;
app.use(bodyParser.urlencoded({extened:true}));
MongoClient.connect(db.url,(err,database) =>{
 if (err) return console.log(err)
//require('./app/routes')(app,{});
//check below line changed
 require('./app/routes')(app, database);
app.listen(port,() => {
    console.log("We are live on"+port); 
});
})

```