3

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); 
});
})
Community
  • 1
  • 1
Herxheimer
  • 31
  • 2
  • You are passing an empty object from server.js to index.js then to note_routes.js as db param where you are trying to use it as db.collection but the db is empty object, it doesn't have collection property. – Molda Jun 17 '17 at 06:26
  • try to check the dependencies in package.json for the mongodb – Naveen Kumar Dec 06 '17 at 10:10

2 Answers2

4

Remove the node_modules folder and change mongodb version of your package.json

"mongodb": "^2.2.33"

and run below code :

npm install
LuFFy
  • 8,799
  • 10
  • 41
  • 59
ranjith
  • 414
  • 1
  • 4
  • 9
-1

change to this require('mongodb').MongoClient;

Mohammed mansoor
  • 743
  • 3
  • 11
  • 18