![](/img/trans.png)
[英]Securely passing variable from client side javascript to nodejs server
[英]Passing associative arrays from nodejs to client side ends up empty
從nodejs服務器端,我編寫了以下代碼
socketIOobj.to(clientID).emit('send-prev-conversation-data',{ prevConversation: JSON.stringify(finalOutputArray) });
在這里,如果我執行console.log(finalOutputArray),我得到以下輸出
[ [ convId: 11,
no: 1,
time: 2016-12-27T17:36:19.000Z,
subjectline: 'message005' ],
[ convId: 10,
no: 2,
time: 2016-12-26T18:02:17.000Z,
subjectline: 'fdf' ],
[ convId: 4,
no: 2,
time: 2016-12-25T09:46:12.000Z,
subjectline: 'cds' ],
[ convId: 3,
no: 4,
time: 2016-12-25T09:33:39.000Z,
subjectline: 'gg2' ] ]
但是,當我嘗試使用以下代碼在客戶端接收finalOutputArray數組值時
socket.on( 'send-prev-conversation-data', function( data ) {
console.log(data.prevConversation);
var aa=JSON.parse(data.prevConversation);
console.log(aa);
console.log(aa[0]);
socket.removeAllListeners('send-prev-conversation-data');
});
我得到的輸出為:
[[],[],[],[]]
Array [ Array[0], Array[0], Array[0], Array[0] ]
Array [ ]
在這里,我的問題是, 如何獲得在nodejs中創建的精確數組,例如:
[ [ convId: 11,
no: 1,
time: 2016-12-27T17:36:19.000Z,
subjectline: 'message005' ],
[ convId: 10,
no: 2,
time: 2016-12-26T18:02:17.000Z,
subjectline: 'fdf' ],
[ convId: 4,
no: 2,
time: 2016-12-25T09:46:12.000Z,
subjectline: 'cds' ],
[ convId: 3,
no: 4,
time: 2016-12-25T09:33:39.000Z,
subjectline: 'gg2' ] ]
在客戶端,以便我可以使用它在客戶端瀏覽器中顯示數據。
好像發生了什么是您要添加的屬性添加到數組的對象,而不是它在描述這個問題,或者這個問題,這幾乎是完全你的問題。
我可以從console.log(finalOutputArray)
的輸出中看出這一點,其中的每個元素都是數組而不是對象。 可能您已經定義了數組元素,如下所示:
var item = [];
item.convId = ...
item.no = ...
...
雖然您可以在Javascript中將非數字鍵添加到數組中,但是如您所見,JSON.stringify不支持此功能,而僅對錯誤的數字鍵進行字符串化。 解決方法很簡單。 只需使用一個對象代替:
var item = {};
item.convId = ...
item.no = ...
...
感謝您提供給我解決問題的提示。 現在,我找到以下解決方案:
以前,我將多維數組聲明為:
var finalOutputArray031 = [];
var tempFlag031 = -1;
// inside some for loop
tempFlag031++;
finalOutputArray031[tempFlag031] = []; // it creates array of array
finalOutputArray031[tempFlag031]['convId'] = tempKey031;
finalOutputArray031[tempFlag031]['no'] = 1;
finalOutputArray031[tempFlag031]['time'] = ii.conversation_time;
After that when I try to
JSON.stringify(finalOutputArray031)
然后我得到的輸出像
[[],[],[],[]]因為,我的多維度數組變成了Array [Array [0],Array [0],Array [0],Array [0]]
If I want to get my multidimenational array value exactly as I created in node.js then, I have
to define like
var finalOutputArray031 = [];
var tempFlag031 = -1;
// inside some for loop
tempFlag031++;
finalOutputArray031[tempFlag031] = {}; // instead of array it creates object
finalOutputArray031[tempFlag031]['convId'] = tempKey031;
finalOutputArray031[tempFlag031]['no'] = 1;
finalOutputArray031[tempFlag031]['time'] = ii.conversation_time;
Then, in client side when I do as below:
var prevConvArray = JSON.parse(data.prevConversation);
console.log(prevConvArray);
for(var i=0;i<prevConvArray.length;i++){
console.log(prevConvArray[i]);
}
Then I get what exactly I want
Array [ Object, Object, Object, Object ]
Object { convId: 11, no: 1, time: "2016-12-27T17:36:19.000Z", subjectline: "goutam dolai goutam dolai" }
Object { convId: 10, no: 2, time: "2016-12-26T18:02:17.000Z", subjectline: "fdf" }
Object { convId: 4, no: 2, time: "2016-12-25T09:46:12.000Z", subjectline: "cds" }
Object { convId: 3, no: 4, time: "2016-12-25T09:33:39.000Z", subjectline: "gg2" }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.