我正在使用节点js使用GCM向Android设备发送推送通知,并且如果我收到来自Google的错误消息,则想要删除设备ID。代码片段已附加并以循环方式运行(i是设备ID的数量)。
我的问题是,如果调用返回错误(错误4),则所有将来的循环都返回if(err4)为true并且删除第一个错误之后的所有id。为什么err4没有重置?
for(var i = 0; i < noofdeviceIds; i++){
// some more code here to create the 'message'.
(function(i) {
gcm.send(message, function(err4, messageId){
if (err4) {
console.log("\nError occured: Notification could not be sent with error: ", err4);//
if(err4 == 'NotRegistered' || err4 == 'InvalidRegistration'){
console.log("\nRemoving the entry from the DB.");
var removeEntryQuery = "DELETE FROM devicereg WHERE deviceregId = '" + deviceregIds[i] + "'";
req._dbConnection.query(removeEntryQuery, function(err5, row) {
if(row!=0){console.log("\nDB returned: ", row);}
});
}
}
else {
console.log("\nMessage sent with message ID: ", messageId);
}
});
})(i);
}