Node js & mongoDB - TypeError: db.collection is not a function

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

There is a similar question, but the answer is incomplete and does not store any keys / values ​​that I put in the postman in mLab. The code I'm trying to do 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 and does not throw db.collection 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); 
});
})

      

+3


source to share


2 answers


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

"mongodb": "^2.2.33"

      



and run the code below:

npm install

      

+4


source


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



-1


source







All Articles