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 5985611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:33:15+00:00 2026-05-22T22:33:15+00:00

I have this part of code in my application. card.getcard(command, function(toproceed,resultscard) { console.log(‘entry other

  • 0

I have this part of code in my application.

 card.getcard(command, function(toproceed,resultscard) {
    console.log('entry other cards api result'+sys.inspect(resultscard));
    if (resultscard.length==0) {
      return proceed(false,{errno:'011','queueno' : request.queueno, message:'there is no access card for particular gib'});
    }

    for (var i=0; i<resultscard.length;i++) {
      console.log('card owner'+resultscard[i].owner);

      //checking that any users is in inside of gib
      server.wrap(function(){
        server.getchannel("channels."+request.gibid+'-I', function(err, channel) {
          if (channel.users) {
            var arr=channel.users.split(',');
            if (functions.in_array(resultscard[i].owner, arr)) {
              response.users.push(resultscard[i].owner);
            }
          }
        });
        if(i==resultscard.length-1) {
          if (response.users.length<=0) {
            //here need to send sorry event that no owner is online
            request._command='sorry';
          } else {
            request._command='knock';
          } 
          return proceed(true,response);
        }
      });

    }
  }); 

while executing this giving me error .

entry other cards api result[ { cardid: 16,
    cardtype: 'A',
    status: 'A',
    refername: 'rahulgib',
    refertype: 'G',
    owner: 'rahul' },
  { cardid: 27,
    cardtype: 'A',
    status: 'A',
    refername: 'rahulgib',
    refertype: 'G',
    owner: 'namita' } ]
card ownerrahul
card ownernamita

node.js:178
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: Cannot read property 'owner' of undefined
    at Object.callback (/home/myhome directory /redisyoungib/lib/yapi.js:271:50)
    at RedisClient.return_reply (/usr/local/lib/node/.npm/redis/0.6.0/package/index.js:384:29)
    at HiredisReplyParser.<anonymous> (/usr/local/lib/node/.npm/redis/0.6.0/package/index.js:78:14)
    at HiredisReplyParser.emit (events.js:64:17)
    at HiredisReplyParser.execute (/usr/local/lib/node/.npm/redis/0.6.0/package/lib/parser/hiredis.js:35:22)
    at RedisClient.on_data (/usr/local/lib/node/.npm/redis/0.6.0/package/index.js:325:27)
    at Socket.<anonymous> (/usr/local/lib/node/.npm/redis/0.6.0/package/index.js:90:14)
    at Socket.emit (events.js:64:17)
    at Socket._onReadable (net.js:673:14)
    at IOWatcher.onReadable [as callback] (net.js:177:10)

I am not getting why it is giving this error ?

get card gives the result from mysql of card

wrap function executed the callback function .

getchannel return the data from redis .

  • 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-22T22:33:16+00:00Added an answer on May 22, 2026 at 10:33 pm

    The functions you’re creating and passing into server.getchannel are closures over the i variable (well, over everything in scope, but it’s i we’re concerned with). They get an enduring reference to i, not a copy of its value as of when the function was created. That means when the function runs, it uses the current value of i, not the value as it was when the function was created. The upshot is that all of those functions will use the same value of i, which is the value as of the end of the loop. Since that’s beyond the end of the array, resultscard[i] is undefined and so trying to read an owner property from it fails. (More about closures: Closures are not complicated)

    So what you want to do is get those functions to close over something that’s a copy of the value of i. The usual way to do that is to have a factory function that creates them and that accepts the value to use as an argument. The factory function creates the callback function, which closes over the argument, whose value doesn’t change.

    Without reading through it too carefully, applying that to your code probably looks something like this:

    card.getcard(command, function(toproceed,resultscard) {
        console.log('entry other cards api result'+sys.inspect(resultscard));
        if (resultscard.length==0) {
          return proceed(false,{errno:'011','queueno' : request.queueno, message:'there is no access card for particular gib'});
        }
    
        for (var i=0; i<resultscard.length;i++) {
          console.log('card owner'+resultscard[i].owner);
    
          //checking that any users is in inside of gib
          server.wrap(function(){
            server.getchannel("channels."+request.gibid+'-I', makeCallback(i));
            // Call the factory function, passing in `i` -----^
            if(i==resultscard.length-1) {
              if (response.users.length<=0) {
                //here need to send sorry event that no owner is online
                request._command='sorry';
              } else {
                request._command='knock';
              } 
              return proceed(true,response);
            }
          });
    
        }
    
        // The factory function    
        function makeCallback(index) {
          return function(err, channel) {
            if (channel.users) {
              var arr=channel.users.split(',');
              // Note we use `index` -- our argument -- not `i` below
              if (functions.in_array(resultscard[index].owner, arr)) {
                response.users.push(resultscard[index].owner);
              }
            }
          };
        }
      }); 
    

    Now the callback we create in makeCallback closes over the index argument for the call that created it, which nothing other changes. We pass i in, and there we are. It’s still a closure over the other things (because of where makeCallback is defined), but it uses index with them so it handles the right entry.

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

Sidebar

Related Questions

No related questions found

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.