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

  • SEARCH
  • Home
  • 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 3977796
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:57:59+00:00 2026-05-20T04:57:59+00:00

UPDATED CODE: i, I’m new to Javascript programming and getting an undefined variable when

  • 0

UPDATED CODE: i, I’m new to Javascript programming and getting an undefined variable when trying to assign a new variable from a method.

I’m using node.js and creating a redis server using the redis-client in the “client variable”.

var redis = require("redis");
var client = redis.createClient();

client.on("error", function (err) {
console.log("Error " + err); });

var numberPosts;

client.get("global:nextPostId", function(err, replies) {
  numberPosts = replies;
  console.log(numberPosts);
});

console.log(numberPosts);

When I call console.log inside the call back function it returns the proper value, however when I call the console.log outside of the callback function it returns “undefined”. I’m trying to assign the value that is inside the callback function to the global variable numberPosts.

Any help is much appreciated, thanks.

Matt

  • 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-20T04:57:59+00:00Added an answer on May 20, 2026 at 4:57 am

    I believe this will work:

    client.get("global:nextPostId", function (err, reply) {
        console.log("Number of posts: " + reply.toString());
    })
    

    The AJAX call is asynchronous so it doesn’t have return value.. instead you have to use callback function and only there you have the value returned by the server method.

    Edit: to assign the return value to global variable, first declare global variable:

    var _numOfPosts = "";
    

    Then:

    client.get("global:nextPostId", function (err, reply) {
         _numOfPosts = reply.toString());
    })
    

    However, the value won’t be available until the AJAX call is finished so your original code can’t work. There is not direct return value to store.

    You can set timer to some reasonable response time, then have the code using the global variable in there.

    Edit II: in order to call the method again once it’s finished, have such code:

    var _nextPostCallCount = 0;
    function GetNextPost() {
       //debug
       console.log("GetNextPost called already " + _nextPostCallCount  + " times");
    
       //sanity check:
       if (_nextPostCallCount  > 1000) {
          console.log("too many times, aborting");
          return;
       }
    
       //invoke method:
       client.get("global:nextPostId", function(err, replies) {
          numberPosts = parseInt(replies.toString(), 10);
          console.log("num of replies #" + (_nextPostCallCount + 1) + ": " + numberPosts);
    
          //stop condition here.... for example if replies are 0
          if (!isNaN(numberPosts) && numberPosts > 0)
             GetNextPost();
       });
    
       //add to counter:
       _nextPostCallCount++;
    }
    GetNextPost();
    

    This will call the method over and over until the result is 0 or you pass some hard coded limit to prevent endless loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I have updated my code quite a bit. I am getting a new
I updated my code with string dates created with new Date and added back
Updated code snipped as asked: int MovieNum=children.size(); String[] MovieName=new String[MovieNum]; String[] MovieCover=new String[MovieNum]; RadioGroup
I have a code in javascript and I am using jQuery Calculation plugin (can
Has anyone out there been using the ASP.NET Membership Provider code with the new
I currently am using a simple javascript code to get a popup of data
I am trying to access textbox value using the jQuery code; $(#pg_textbox).val(); But it
THE CODE BELOW IS UPDATED CODE THAT WORKS I am getting a syntax error
Here's my updated code trying to get my form to send me the email.
I using php sessions in my project,i updated code:added some values to session.now i

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.