0
我想要的功能,使一個新的JSON對象,看起來如此:Node.js&Redis&For循環與藍鳥承諾?
{ T-ID_12 : [{ text: "aaaaa", kat:"a" }], T-ID_15 : [{ text: "b", kat:"ab" }], T-ID_16 : [{ text: "b", kat:"ab" }] }
這{ text: "aaaaa", kat:"a" }
在thesenjsondata這T-ID_12
是陣列Thesen_IDS的條目。我的解決方案到目前爲止是:
function makeThesenJSON(number_these, Thesen_IDS){
var thesenjsondata;
var thesenids_with_jsondata = "";
for (i = 0; i < number_these; i++){
db.getAsync(Thesen_IDS[i]).then(function(res) {
if(res){
thesenjsondata = JSON.parse(res);
thesenids_with_jsondata += (Thesen_IDS[i] + ' : [ ' + thesenjsondata + " ], ");
}
});
}
var Response = "{ " + thesenids_with_jsondata + " }" ;
return Response;
}
我知道,for循環比db.getAsync()更快。我如何使用redis權限的藍鳥承諾,以便返回值具有我想要的所有數據?
函數的返回,現在是{ 「isFulfilled」:假 「isRejected」:假 } – Arzan0
見我如何使用這個功能 –
THX編輯,它的工作,但我不得不「var thesenjsondata = JSON.parse(res);」更改爲「var thesenjsondata = res;」 – Arzan0