I wanted to export the const database inside the MongoClient.connect function. I just tried to execute this code. However, this did not work.
module.exports.database = database
Does anyone have any guidance? I am trying to export this to another file that functions as a web scraper and I want to use this line to insert in the database.
database.collection('postInfo').insertMany(object)
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
require('dotenv').config();
// Connection URL
const url = process.env.MongoDB_URL;
let a = {};
//database name
const db = 'DB';
//use connect to connect to db
MongoClient.connect(url, {useNewUrlParser: true}, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const database = client.db(db);
client.close();
})