These are chat archives for Automattic/mongoose

20th
Mar 2017
Paul "Joey" Clark
@joeytwiddle
Mar 20 2017 11:53

@rajiff In our project, we define schemas but we do not turn them into models in the same file. The models are created in a different file, after importing the schema. But that doesn't help you if you want to export a retrieval method.

Perhaps dependency injection could help you. Pass the mongoose connection into the module when you initialise it. So your module would have to export a function.

// userModelFactory.js
module.exports = function (connection) {
  var schema = ...; // put this in a different file if you want
  var userModel = connection.model('user', schema, 'user');
  var getUser = ...;
  return {
    getUser: getUser,
  };
};

// app.js
var userModule = require('./userModelFactory')(mongooseConnection);

userModule.getUser(...);
Ilya Borovitinov
@icehaunter
Mar 20 2017 13:30
Hey guys, I can't figure out why do I have MongoError: Authentication failed.
This is the whole code:
let url = 'mongodb://localhost:27017/liaclicker'

mongoose.connect(url)
The same connection url works for
var mongo = require('mongodb').MongoClient
mongo.connect('mongodb://localhost:27017/liaclicker', (err, db) => { console.log('con'); db.close() })
Figured it out