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 .
The functions you’re creating and passing into
server.getchannelare closures over theivariable (well, over everything in scope, but it’siwe’re concerned with). They get an enduring reference toi, not a copy of its value as of when the function was created. That means when the function runs, it uses the current value ofi, not the value as it was when the function was created. The upshot is that all of those functions will use the same value ofi, which is the value as of the end of the loop. Since that’s beyond the end of the array,resultscard[i]isundefinedand so trying to read anownerproperty 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:
Now the callback we create in
makeCallbackcloses over theindexargument for the call that created it, which nothing other changes. We passiin, and there we are. It’s still a closure over the other things (because of wheremakeCallbackis defined), but it usesindexwith them so it handles the right entry.