我的應用程序中有這部分代碼。爲什麼async在循環中的node.js中發生錯誤
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);
}
});
}
});
當執行這給我錯誤。
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)
我不明白爲什麼會出現此錯誤?
獲取卡片給出從卡片的結果
包裝功能執行回調函數。
getchannel從redis中返回數據。
@TJ我應該怎麼做才能刪除錯誤?並正確執行我的程序 – XMen 2011-06-06 09:14:52
@Rahul:我已經添加了一些說明和常見方法的代碼。 – 2011-06-06 09:17:06
@TJ我已更新我的代碼acc。回答,並測試它,給我錯誤,'resultscard沒有定義',我應該怎麼做這個 – XMen 2011-06-06 09:31:49