0

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);
});
4
  • Don't rush SO, read official docs first Commented Apr 1, 2016 at 4:45
  • 1
    Is your question really "how does async.waterfall work?" ? Commented Apr 1, 2016 at 16:14
  • Yes and no. I understood async waterfall at a low level but I didn't understand how to make them work with functions from other modules. Commented Apr 1, 2016 at 16:17
  • In your scenario, I would not even use async - I've updated my answer below to show you how you can invoke functions from other modules. Commented Apr 1, 2016 at 16:28

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have edited to include the require, my question was never meant to target that piece this is why i specified the comment "how does this look". I don't understand how to call this as a function within the waterfall, how the parameter pass in, and how the callback works.
1

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
    });
});

1 Comment

I had thought about this, but in my case there would be about 4 callbacks creating a callback hell. If it were just one you are 100% correct. Therefore I am using async to keep the code somewhat flat. I am too new to JS to use promises for now to solve this problem.
1

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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.