在我的设置中,应将{useNewUrlParser:true}放在哪里?

时间:2019-04-28 00:39:32

标签: javascript mongodb express

我不知道需要在哪里添加{useNewUrlParser:true}。我应该放在哪里?还是应该更改连接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('/');
});

1 个答案:

答案 0 :(得分:0)

您可以将选项作为第二个参数传递给MongoClient():

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

您可以在此处找到所有可用的选项:http://mongodb.github.io/node-mongodb-native/3.2/api/MongoClient.html