11
I've been trying for the last hour to write a user module for passport.js with the findOne, findOneOrCreate, etc. methods, but can't get it right.
User.js
var User = function(db) {
this.db = db;
}
User.prototype.findOne(email, password, fn) {
// some code here
}
module.exports = exports = User;
app.js
User = require('./lib/User')(db);
User.findOne(email, pw, callback);
I've been through dozens of errors, mostly
TypeError: object is not a function
or
TypeError: Object function () {
function User(db) {
console.log(db);
}
} has no method 'findOne'
How do I create a proper module with these functions without creating an object/instance of User?
Update
I went over the proposed solutions:
var db;
function User(db) {
this.db = db;
}
User.prototype.init = function(db) {
return new User(db);
}
User.prototype.findOne = function(profile, fn) {}
module.exports = User;
No luck.
TypeError: Object function User(db) {
this.db = db;
} has no method 'init'