您好我正在尝试将数据推送到已经定义了密钥的JSON对象,但我仍然在我有警报(“3”)和警报(“6”)的行中得到未被捕获的typeError
server.on('update_message_from_server', function(data){
if(data.message_to!=false){
alert("1");
if(data.message_from==<?php echo $session_user_id;?>){
alert("2");
if(message_store.hasOwnProperty(data.message_to_email)){
alert("3");
message_store[data.message_to_email].push((data));
str2 = '<div id ="recent_message_log_entry'+data.message_to_email+'"><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
$(document.getElementById("recent_message_log_entry"+data.message_to_email)).replaceWith(str2);
}else{
alert("4");
message_store[data.message_to_email]= new Array();
message_store[data.message_to_email]=data;
string = '<div><div>'+data.message_to_email+'</div><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
$("#new_updates").append(string);
}
}else{
alert("5");
if(message_store.hasOwnProperty(data.message_from_email)){
alert("6");
message_store[data.message_from_email].push((data));
str2 = '<div id ="recent_message_log_entry'+data.message_from_email+'"><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
$(document.getElementById("recent_message_log_entry"+data.message_from_email)).replaceWith(str2);
}else{
alert("7");
message_store[data.message_from_email]= new Array();
message_store[data.message_from_email]=data;
string = '<div><div>'+data.message_from_email+'</div><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
$("#new_updates").append(string);
}
}
}else{
alert("8");
alert("Email Does not exist "+data.message_to_email);
}
alert(JSON.stringify(message_store));
});
我如何纠正这个问题?
提前致谢
答案 0 :(得分:2)
如果未定义message_store[data.message_from_email]
,则首先为其分配一个空数组,然后将数据推送到数组,而不是用data
覆盖数组
错误在行
message_store[data.message_from_email]= new Array();
message_store[data.message_from_email]=data;
应该是
message_store[data.message_from_email] = new Array();
message_store[data.message_from_email].push(data);
或缩短
message_store[data.message_from_email]= [data];
所以
server.on('update_message_from_server', function(data){
if(data.message_to!=false){
alert("1");
if(data.message_from==<?php echo $session_user_id;?>){
alert("2");
if(message_store.hasOwnProperty(data.message_to_email)){
alert("3");
message_store[data.message_to_email].push((data));
str2 = '<div id ="recent_message_log_entry'+data.message_to_email+'"><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
$(document.getElementById("recent_message_log_entry"+data.message_to_email)).replaceWith(str2);
}else{
alert("4");
message_store[data.message_from_email]=[data];
string = '<div><div>'+data.message_to_email+'</div><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
$("#new_updates").append(string);
}
}else{
alert("5");
if(message_store.hasOwnProperty(data.message_from_email)){
alert("6");
message_store[data.message_from_email].push((data));
str2 = '<div id ="recent_message_log_entry'+data.message_from_email+'"><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
$(document.getElementById("recent_message_log_entry"+data.message_from_email)).replaceWith(str2);
}else{
alert("7");
message_store[data.message_from_email]=[data];
string = '<div><div>'+data.message_from_email+'</div><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
$("#new_updates").append(string);
}
}
}else{
alert("8");
alert("Email Does not exist "+data.message_to_email);
}
alert(JSON.stringify(message_store));
});