gpt4 book ai didi

javascript - 如何摆脱 Object # has no method 'push' 错误?
转载 作者:行者123 更新时间:2023-11-28 00:06:40 25 4
gpt4 key购买 nike

您好,我正在尝试将数据推送到已经定义了键的 JSON 对象,但我仍然在我有警报(“3”)和警报(“6”)的行收到未捕获的类型错误

 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));
});

我该如何纠正这个问题?

提前致谢

最佳答案

如果 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));
});

关于javascript - 如何摆脱 Object #<Object> has no method 'push' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18117517/

25 4 0