i have a database.js
file. in this file i have 3 functions named connect
, getDb
and disconnect
.
when i use exports
(the below code) , the require
function loads nothing! and when i use module.exports, every thing goes in the right way! whats wrong with exports
here?
// my database.js file
const MongoClient = require('mongodb').MongoClient;
const database_name = 'car-repair';
const uri = `mongodb://localhost:27017/${database_name}`;
let database;
const connect = () => {
let options = {
useNewUrlParser: true,
useUnifiedTopology: true
};
try {
MongoClient.connect(uri, options, (err, db) => {
database = db;
console.log('connected to database.');
});
} catch (e) {
console.log(e);
}
};
const getDB = () => database;
const disconnect = () => database.close();
exports = {
connect, getDB, disconnect
};
if i use the code like the below, it returns undefined
// my app.js file
const database = require('./database');
console.log(database.connect); // undefined!
when i change the exports
to module.exports
everything works properly.
module.exports = {
connect, getDB, disconnect
};
whats wrong with exports
?