Node.js: Undefined return value after database query

I got a file newuser.js (node.js environment featuring a mongodb database managed via mongoose) containing the following code:

//newuser.js

//basically creates new user documents in the database and takes a GET parameter and an externally generated random code (see randomcode.js)

[...]
var randomCode = require ('randomcode');

var newTempUser = new tempUser({name: req.body.name, vericode: randomCode.randomveriCode(parameter)
});

newTempUser.save(function (err){
    //some output
});

//randomcode.js

//creates a random sequence of characters (=vericode), checks if code already exists in DB and restarts function if so or returns generated code

exports.randomveriCode = function randomveriCode(parameter){
    [...]

    var TempUser = conn.model('TempUser', TempUserSchema);

    TempUser.count({vericode: generatedcode}, function(err, counter){

        if (counter=='0'){
            return generatedcode;
        }else{
            randomveriCode(parameter);  
        }
    });
};

Problem is, that newuser.js throws an error as variable vericode is 'undefined' (thus mongoose model validations fails). The error does not occur if I skip the database query and instantly return the generated code (which in fact has got a value as verified by several console.log instructions). It occurs to me that the db query takes to long and empty or null value returned before query is complete? I thought about introducing promises unless you got any other suggestions or hints what may cause this behaviour? Kind regards

Igor

Since querying the database is a non-blocking operation, you cannot expect the function call to return the value from the database immediately. Try passing in a callback instead:

// newuser.js
var randomCode = require('randomcode');

randomCode.randomveriCode(parameter, function(err, code) {
  if (err) throw err; // TODO: handle better

  var newTempUser = new tempUser({name: req.body.name, vericode: code});

  newTempUser.save(function (err){
    //some output
  });
});



// randomcode.js
exports.randomveriCode = function randomveriCode(parameter, cb) {
  var TempUser = conn.model('TempUser', TempUserSchema);

  TempUser.count({vericode: generatedcode}, function(err, counter) {
    if (err) return cb(err);

    if (counter == '0') {
      cb(null, generatedcode);
    } else {
      randomveriCode(parameter, cb);
    }
  });
};

your randomveriCode function contains calls to an asynchronous function and therefore, your function really needs to provide a callback argument like this:

exports.randomveriCode = function randomveriCode(parameter, callback){
  [...]

  var TempUser = conn.model('TempUser', TempUserSchema);

  TempUser.count({vericode: generatedcode}, function(err, counter){
    if(err) return callback(err);
    if (counter=='0'){
        return callback(null, generatedcode);
    }else{
        randomveriCode(parameter, callback);  
    }
  });
};

You'd then call it like so:

var randomCode = require ('randomcode');

randomCode(function(err, vericode){
  if(err) throw err;
  var newTempUser = new tempUser({name: req.body.name, vericode: vericode});
  newTempUser.save(function(err,newUser){
    //do something here
  });
});

Btw - you could also use a synchronous function to create a GUID. See https://www.npmjs.org/package/node-uuid.