Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6766739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:52:03+00:00 2026-05-26T14:52:03+00:00

Newbie question. Why is this JavaScript function returning undefined? var redis = require(redis), client

  • 0

Newbie question. Why is this JavaScript function returning undefined?

var redis = require("redis"), client = redis.createClient();
function generatePageUrl() { 
    var randomStr = randomInt.toString(32);
    // Check whether this URL is already in our database;
    client.smembers("url:" + randomStr, function (err, data ) {
        if (data.length != 0) {
            // URL already in use, try again
            return getPageUrl();
        }
        return randomStr;
    });
}
var page_url = generatePageUrl();
// add it to the database, etc

I guess it must be getting to the end and returning before it reaches the inside of client.smembers.

But I really need to check the contents of the Redis set before it returns: can I get it to return from inside the callback? If not, what can I do?

Also, advice on the way I’ve used this function recursively would be welcome – I’m not sure it’s completely sensible 🙂

Thanks for helping out a newcomer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:52:03+00:00Added an answer on May 26, 2026 at 2:52 pm

    You can’t return from inside a callback. Do it like this:

    var redis = require("redis"), client = redis.createClient();
    function generatePageUrl(cb) { 
        var randomStr = randomInt.toString(32);
        // Check whether this URL is already in our database;
        client.smembers("url:" + randomStr, function (err, data ) {
            if (data.length != 0) {
                // URL already in use, try again
                getPageUrl(cb);
            }
            cb(randomStr);
        });
    }
    generatePageUrl(function(page_url){
        // add it to the database, etc
    });
    

    If you don’t like this style, you might want to consider streamlinejs – it makes you able to write your code like this:

    var redis = require("redis"), client = redis.createClient();
    function generatePageUrl(_) { 
        var randomStr = randomInt.toString(32);
        // Check whether this URL is already in our database;
        var data = client.smembers("url:" + randomStr, _);
        if (data.length != 0) {
            // URL already in use, try again
            return getPageUrl(_);
        }
        return randomStr;
    }
    var page_url = generatePageUrl(_);
    // add it to the database, etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a newbie question: I have a pre-existing function that I would like
This is a newbie question: Can the following HTML/JavaScript code be further simplified by
This is a newbie question. I have two javascript and one html files. When
This may be a newbie question. I've used javascript for years but have only
Total newbie question but this is driving me mad! I'm trying this: myInt =
This is a sql newbie question. Basically, I want an extra column to return
This is a total newbie question, so thanks in advance. I'm trying to get
This is a very newbie question and i will probably get downvoted for it,
I realize this is probably a hopelessly newbie question, but what is the difference
This is a real newbie question. I have simple app that selects a picture

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.