0

I'm using the function below inside express js, but I can't return the value of pw.generate. Anyone knows why? I'm kinda new to JavaScript.

  // create new xkcdPassword object
  var pw = new xkcdPassword();

  // set options for password generator
  var options = {
      numWords: 1,
      minLength: 5,
      maxLength: 8
  };

  var password = pw.generate(options, function(error, result) {
    if(error){
      return error;
    }

    // below variable now contains a password like "puppy"
    var newPassword = result.join(' ');

    // This does not work, somehow I cannot return the variable
    return newPassword;
  });

  // below returns an undefined value
  console.log(password);

So the variable "newPassword" does contain the password, inside pw.generate. I cannot use it outside pw.generate. Is it because the function which need to return the password, is used as a paramater?

EDIT: @scx gave me the solution for using a callback, because of the combination of the asynchronous and synchronous methods. I'm using a promise now as a callback, works great, this is my updated code:

function updatePassword(user) {
  var deferred = Q.defer();

  // create new xkcdPassword object
  var pw = new xkcdPassword();

  // set options for password generator
  var options = {
      numWords: 1,
      minLength: 5,
      maxLength: 8
  };

  var password = pw.generate(options, function(error, result) {
    if(error){
      return error;
    }
    var newPassword = result.join(' ');

    user
      .updateAttributes({
        password: newPassword
      })
      .success(function(){
        deferred.resolve(newPassword);
      })
      .error(function() {
        deferred.reject();
      });
  })
  return deferred.promise;
}

module.exports = {
  passwordReset: function(req, res, next) {
    updatePassword(user)
    .then(function(password){
      next();
    }, function(){
      // error
    });
  }
}
Erik van de Ven
  • 4,747
  • 6
  • 38
  • 80
  • 1
    Please share the full code of `xkcdPassword()`... – BenM Aug 06 '14 at 08:55
  • 1
    where are you expecting the return value, You will get return value in `pw.generate` function where you will be calling callback. – Mritunjay Aug 06 '14 at 08:56
  • you have to return it from `pw.generate()` & you will get in `password`. – Mritunjay Aug 06 '14 at 09:10
  • not working, it allows me only to use the new password, inside pw.generate, but that's not the way to do it ofcourse..... Is it because the function which need to return the password, is used as a paramater? – Erik van de Ven Aug 06 '14 at 09:18

1 Answers1

0

According to this example you miss importing xkcd-password, try adding following before your code:

var xkcdPassword = require('xkcd-password');
var pw = new xkcdPassword();

Btw did you installed xkcd-password with npm correctly? If above will not help try installing with npm like bellow.

npm install xkcd-password

Update: Please check this answer and you will understand why you do not have access to that variable outside: How to return value from an asynchronous callback function?

Community
  • 1
  • 1
scx
  • 2,749
  • 2
  • 25
  • 39
  • yes I did. It does generate a password within the object, but I cannot return it. I can only use it inside the pw.generate() – Erik van de Ven Aug 06 '14 at 09:07
  • I have updated the answer. you should continue your work inside your callback function and that is it. – scx Aug 06 '14 at 09:28
  • 1
    Thank you for the answer. Especially the part "This is impossible as you cannot use an asynchronous call inside a synchronous method." seems very straight forward. I guess I'll get it done now :) – Erik van de Ven Aug 06 '14 at 09:41