如何摆脱 Object #<Object> has no method 'push' 错误?
Posted
技术标签:
【中文标题】如何摆脱 Object #<Object> has no method \'push\' 错误?【英文标题】:how to get rid of Object #<Object> has no method 'push' error?如何摆脱 Object #<Object> has no method 'push' 错误? 【发布时间】:2013-08-09 15:55:45 【问题描述】:您好,我正在尝试将数据推送到已定义密钥的 JSON 对象,但在我有 alert("3") 和 alert("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));
);
我该如何解决这个问题?
提前致谢
【问题讨论】:
【参考方案1】:如果未定义message_store[data.message_from_email]
,则首先为其分配一个空数组,然后用data
覆盖数组而不是将数据推送到数组中
bug 就在这条线上
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));
);
【讨论】:
以上是关于如何摆脱 Object #<Object> has no method 'push' 错误?的主要内容,如果未能解决你的问题,请参考以下文章
python3.4.0 input Can't convert 'int' object to str implicitly 如何正确输出?
神秘的 GObject 警告:断言 `G_IS_OBJECT (object)' 失败
如何在 Node.js console.log() 中获取完整对象,而不是“[Object]”?
g_dbus_proxy_new_for_bus_sync:断言“g_variant_is_object_path(object_path)”失败