I am beginner level in Node JS and here I am trying to connect to MongoDB.
I am following one tutorial and based on that, I have following project structure:
- server
- config
- db.js
- index.js
- index.js
My mongoose dependency version is: 5.6.9
Inside my db.js, I have following code of lines:
const mongoose = require('mongoose');
// connection to mongoose
export default () => {
mongoose.connect('mongodb://localhost/dbCollection', function(err, db) {
if (err) {
console.log('Unable to connect to the server. Please start the server. Error:', err);
} else {
console.log('Connected to Server successfully!');
}
});
}
And inside the index.js inside the server folder, I have
import express from 'express';
import dbConfig from './config/db';
const app = express();
dbConfig();
But I am getting error
_mongoose2.default.connection().once('open', function () {
^
TypeError: _mongoose2.default.connection is not a function
at exports.default
node --experimental-modules index.mjsDocs. 2/ Use Es5 syntax :module.exports = () => {...}andconst dbConfig = require('./config/db');