Reputation: 143
I am miss understanding this concept in nodejs. I want to place a function in a folder at ./models/user lets say to represent a model I use for user. Then I want to use these as functions somewhere else. The issue I always run into is when do something like user.something it doesn't handle like a function. I am misunderstanding how this works.
The model would look something like this:
//model/user.js
function User() {
this.foo = null;
}
User.prototype.hashPass = function (password, callback) {
//Code that hashes a password
callback(err, hash);
};
User.prototype.insertUser = function (email, password, callback) {
//Code that inserts a user and returns some 'done' callback
callback(err, done);
};
module.exports = User;
And somewhere else in my program lets say passport.js I want to do this:
//config/passport.js
var User = require('../models/user);
var user = new User();
async.waterfall([
//how does this look
//EDIT ATTEMPTED HERE
user.hashPass(password, function(err, result) {
}),
user.insertUser(result, function(err, rows) {
})
], //some callback );
Made some edits to help to clarify what I am trying to accomplish here.
EDIT: This link shows how to do async waterfalls with multiple callbacks
Code based on EDIT / Understanding:
async.series([
function(callback) {
user.hashPass(password, function(err, result) {
callback(err,result);
})
}
], function(err, result) {
if (err) return err;
console.log('test',result);
});
Upvotes: 0
Views: 474
Reputation: 1707
The only thing you need to change in your code is to replace the line
module.exports = User;
in your passport.js
file by
var User = require('../model/User');
Then you can invoke the functions on user:
user.hashPass(password, function(err, result) {
user.insertUser(result, function(err, rows) {
// do something with rows here
});
});
Upvotes: 1
Reputation: 21
It's not working because you have to 'require' your module in the file you want to use it in and you're creating object methods on a constructor function that does not exist. Instead, you could create a user object (not a constructor function) and set each function to an object property, like this:
//module file
var user = {
hashPass: function(password, callback){
//password-hashing function
},
insertUser: function(email, password, callback){
//new user function
}
};
module.exports = user;
Then, in whatever place you want to use it, you do so like this:
//some other file
var user = require(path-to-module);
user.hashPass(); //pass in all parameters (password, callback)
user.insertUser(); //pass in all parameters (password, callback)
The only potential hang-up about this method is that you'll have to define all of your parameters before calling either object property.
Upvotes: 2
Reputation: 720
When we have been requiring custom modules we require them by path .
var User = require('config/passport');
var user = new User();
async.waterfall([
//how does this look
//do hashPass here
,
//do insertPass here
], //some callback );
Upvotes: 1