0
這裏是一個片段:經由node.js的回覆於返回JSON有意外的結果
objects = [];
client.keys 'objects*', (err,keys) ->
for (i = 0; i < keys.length; i++){
client.hgetall(keys[i], function(err, obj) {
objects.push(obj);
if (i === keys.length){
response.writeHead(200, {'Content-Type': 'application/json'});
console.log(JSON.stringify(objects));
response.write(JSON.stringify(objects));
response.end();
}
}
}
的變種對象是我通過經由節點redis的查詢redis的填充對象的數組,每組6屬性。在控制檯中,我得到了我期望的。然而,在一個客戶端中,我只收到一個只包含最後一個對象的數組。想知道它是否是客戶端的deseralizer,我在Fiddler中捕獲它,它仍然是一個單一對象的數組。
下面是我通過的console.log在控制檯中:
[{"prop1":"11","prop2":"12","prop3":"13","prop4":"14","prop5":"15","prop6":"16"}, {"prop1":"21","prop2":"22","prop3":"23","prop4":"24","prop5":"25","prop6":"26"},{"prop1":"31","prop2":"32","prop3":"33","prop4":"34","prop5":"35","prop6":"36"},{"prop1":"41","prop2":"42","prop3":"43","prop4":"44","prop5":"45","prop6":"46"},{"prop1":"51","prop2":"52","prop3":"53","prop4":"54","prop5":"55","prop6":"56"},{"prop1":"61","prop2":"62","prop3":"63","prop4":"64","prop5":"65","prop6":"66"}]
我必須做一些錯誤的...
你可以舉一個被序列化的對象的例子嗎? – 2012-01-12 16:41:15
我編輯了我的問題以包含更多詳細信息並希望能夠解決您的評論。另外,我原來(錯誤地)聲明我只看到* first *對象,實際上我只是得到* last *之一。 – CircusNinja 2012-01-12 17:07:01
您的異步代碼錯誤。 'if(i === keys.length)'這一行總是會返回true,因爲你正在檢查'client.hgetall'是否已經被調用'keys.length'次,而不是如果調用已經完成。你應該檢查'(objects.length === keys.length)' – fent 2012-01-13 16:54:12