Attempting to develop a basic module for organizing accounts on a website seemed like a straightforward task. I thought it would be as simple as creating a file with some functions, wrapping it in module.exports, and everything would work smoothly. However, it turned out that the reality was quite different - Bob is definitely not my uncle.
I created the following file:
module.exports = function(lameAccount) {
lameAccount.initAccount( function ( myId, myName, done) {
console.log("Will create" + myId);
console.log("Name is " + myName);
done(null,false,"This is just a hello");
})
}
In the section where it is needed (the api router)
var lameAccountant = require('../modules/lameaccount.js')
And then call our function
lameAccount.initAccount(blockRecord._id, blockRecord.betName)
However, when attempting to utilize this functionality, an error occurs:
/Users/bengtbjorkberg/WebstormProjects/Challange/node_modules/mongoose/node_modules/mpromise/lib/promise.js:108
if (this.ended && !this.hasRejectListeners()) throw reason;
^
TypeError: undefined is not a function
at EventEmitter.<anonymous> (/Users/bengtbjorkberg/WebstormProjects/Challange/routes/api.js:119:22)
... (error details continue)
============== Edit in regards to first answer ============
If this is indeed an anonymous function (which I believe it is), why does it function as intended in the following code snippets:
// expose this function to our app using module.exports
module.exports = function(passport) {
// Passport session setup
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
and also within the API implementations:
module.exports = function(app, passport) {
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(bodyParser.json())
// Route to retrieve list of users
app.get('/api/userList', function(req, res){
User.find({}, {'_id':1, 'userName':1},function(err, users) {
if (err)
res.send(err)
res.json(users);
})
});
Does app.use allow for the inclusion of anonymous functions?