Undefined return value mongoose / nodejs recursive function

ddtraceweb

i want use a function recursive to look if the usernames exist in mongodb with mongoose and nodejs.

I use the callback, but i don't understand why my function return undefined result. Could you help Me ?

Thanks ;)

var mongoose = require('mongoose');
var debug = require('debug')('gc:model:User');

var UserSchema = new Schema({

  username: {type: String, required: true, trim: true, index: {unique: true, dropDups: true}},
  email: {type: String, trim: true},

  role: {type: String, required: true, default: 'user'},

});

generateUsername = function (username, number) {
  'use strict';

  var i = 0;
  var usernames = [];
  usernames.push(username);

  while (i < number) {
    var count = parseInt(usernames[i].substr(-1));
    if (count >= 0) {
      count += 1;
    } else {
      count = 0;
    }
    usernames.push(usernames[i].substring(0, count === 0 ?     usernames[i].length : usernames[i].length - 1) + count);
    i++;
  }

  return usernames;
};


findUniqueUsername = function (usernames, cb) {
  'use strict';
  if (usernames.length === 0) {
    return cb(null);
  }

  // If one of the username is undefined, go the next one
  if (typeof usernames[0] === 'undefined') {
    usernames.shift();
    findUniqueUsername(usernames);
  }

  _db.User.findOne({'username': usernames[0]}).exec(function (err, user) {
    if (err) return cb(err);

    if (user) {
      debug('Bu ! => ', usernames[0]);
      usernames.shift();
      findUniqueUsername(usernames);
    }
    else {
      debug('GooD ! => ', usernames[0]); // Value OK i have
      return usernames[0]; // Value Not OK undefined :(
    }
  });

};

var namestart = "jobs";

var usernameTries = generateUsername(namestart, 100);
var username = findUniqueUsername(usernameTries); // is undefined
Eydrian

The variable username will stay undefined, as you are preforming an async call.

If you start by generating 100 usernames, you can query all of them, and then filter out the non-existing. You can still use this recursive, by calling findUniqueUsernames again and passing the callback.

'use strict';

const mongoose = require('mongoose');

let UserSchema = new Schema({

  username: {
    type: String,
    required: true,
    trim: true,
    index: {
      unique: true,
      dropDups: true}
    },
  email: {
    type: String,
    trim: true
  },

  role: {
    type: String,
    required: true,
    default: 'user'
  },

});

var generateUsernames = function (username, number) {

  let usernames = new Array(number - 1)
  .fill(username)
  .map((username, index) => username + index);

  usernames.unshift(username);

  return usernames;
};

var findUniqueUsernames = function (usernames, callback) {
  if (usernames.length === 0) return callback(null, null);

  _db.User.find({
    username: {
      $in: usernames
    }
  })
  .select('username')
  .exec((err, results) => {
    if (err) return callback(err, null);
    else {
      let existingUsernames = results.map(r => r.username);
      let uniqueUsernames = usernames
        .filter(username => existingUsernames.indexOf(username) < 0);
      return callback(null, uniqueUsernames);
     // as an alternative, you could check here, 
     // if uniqueUsernames.length === 0
     // and use findUniqueUsernames recursive by passing the same callback again
     // return findUniqueUsernames(newSetOfPossibleUsernames, callback)
    }
  });
};

var namestart = 'jobs';

var usernameTries = generateUsernames(namestart, 100);

findUniqueUsernames(usernameTries, (err, uniqueUsernames) => {
  if (err) {
    console.log(err);
  } else {
    console.log(uniqueUsernames);
  }
});

In a more mongoosie world, I would make findUniqueUsernames at least a static method of UserSchema:

UserSchema.statics.findUniqueUsernames = function( ...

and replace _db.User.find( ... with

this.find( ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Delay in return value - nodejs & mongoose

Recursive function returning undefined value

Function return value undefined

Nodejs Mongoose Saving model undefined is not a function

Why does this recursive function return undefined?

Why is the recursive function returning undefined on return array?

Javascript return undefined of recursive function with Date()

Why does my recursive function return "undefined"?

Nodejs Mongoose Exports Function return 2 values

Javascript: recursive function returns undefined for existing value

Function return value returns as undefined

nodejs function doesnt return a value

NodeJS Mongoose - not returning value from async function

Unexpected return value from recursive function

Recursive Function return value in vb script

How to terminate a recursive function and return a value

String return value in recursive function in java

Why does this recursive function return the correct value?

how to return a value from recursive function in python

How does recursive function return its value?

recursive function cannot return value as expected

Return value from Linux bash recursive function

Unable to return correct value of a variable in a recursive function

Why does this recursive function return the wrong value?

Correctly return Promise value from recursive function

message: 'Cast to number failed for value "undefined" at path in nodejs mongoose express

Trying to return up a recursive combinations function without getting 'undefined'

Recursive function returns undefined regardless of enough return statements

Recursive function to return random element in an array returning undefined