Friday, 3 July 2020

How requiring mongoose object always give us an access to our current connection?

Hello, I was wondering how just requiring mongoose, in any other file than our main one, give us an access to current mongoose connection without even passing the 'main instance' from the main server file. I know you might not understand what I'm asking for (becouse it's hard for me to demystify it), so I will try to explain it by this simple example:// my main server.js file const mongoose = require('mongoose'); mongoose.connect(process.env.DATABASE, { useNewUrlParser: true, useUnifiedTopology: true, }); require('./otherFile.js')(); // otherFile.js const mongoose = require('mongoose'); module.exports = () => { console.log(mongoose.connection); }; So I'm console logging mongoose connection object by the otherFile.js and it has got for example host or portproperty that is pointing to the same adress specified in server.js file. So it is aware of this mongoose object from server.js but I did not pass it to the function exported from the otherFile.js - so how is it working? Why souldn't we do something like this:// my main server.js file // ... require('./otherFile.js')(mongoose); // otherFile.js module.exports = (mongoose) => { console.log(mongoose.connection); };

Submitted July 03, 2020 at 12:29PM by bartekw2213

No comments:

Post a Comment