javascript - how to get rid of Object #<Object> has no method 'push' error? -
hi trying push data on json object has key defined still getting uncaught typeerror @ line have alert("3") , alert("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 not exist "+data.message_to_email); } alert(json.stringify(message_store)); });
how rectify this?
thanks in advance
if message_store[data.message_from_email]
not defined, first assigning empty array it, instead of pushing data array overwriting array data
the bug in line
message_store[data.message_from_email]= new array(); message_store[data.message_from_email]=data;
it should be
message_store[data.message_from_email] = new array(); message_store[data.message_from_email].push(data);
or shorten
message_store[data.message_from_email]= [data];
so
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 not exist "+data.message_to_email); } alert(json.stringify(message_store)); });
Comments
Post a Comment