0

I can't figure out where I need to add { useNewUrlParser: true}. Where should I place it? Or should I change the way I connect to Mongo?

var express = require('express');
var router = express.Router();
var assert = require('assert');


var url = 'mongodb://urlformyconnection;
const MongoClient = require('mongodb').MongoClient;
const objectId = require('mongodb').ObjectID;
const client = new MongoClient(url);
const dbName ='transactions';
.....
router.post('/insert', function (req, res, next){

  var item = {
    firstN: req.body.firstN,
    lastN: req.body.lastN,
    ccNum: req.body.ccNum,
    cvc: req.body.cvc,
    zip: req.body.zip,
    amount: num
  };

  MongoClient.connect(url, function(err, client)
  {assert.equal(null, err);
    const db = client.db(dbName);
    db.collection('transac-info').insertOne(item, function (err, result) {
      assert.equal(null, err);
      console.log('Transaction Inserted');
      client.close();
    });
  });

  res.redirect('/');
});
  • Note that the *preferred* connection method is using `MongoClient.connect()` calling the *static* `connect()` method, and that your usage of `new MongoClient` is actually redundant by the later call. Most importantly **do not** connect and disconnect within the scope of your request handlers. Database connections should persist for the lifecycle of your application. See [How to properly reuse connection to Mongodb across NodeJs application and modules](https://stackoverflow.com/q/24621940/2313887) for details on how to share the connection correctly. – Neil Lunn Apr 28 '19 at 01:53

1 Answers1

0

You can pass your options as second parameter to MongoClient():

const options = {useNewUrlParser: true};
const client = new MongoClient(url, options);

You can find all available options here: http://mongodb.github.io/node-mongodb-native/3.2/api/MongoClient.html