.push not working for angularjs and pubnub -


i working on pubnub chat. , trying push new message array of messages. have array of message objects like

object {0: object, 1: object, 2: object, 3: object, 4: object, 5: object, 6: object, 7: object, 8: object, 9: object, 10: object, 11: object, 12: object, 13: object, 14: object, 15: object, 16: object, 17: object, 18: object, 19: object} 

each object contains data like

object {content: "asd", date: "2016-08-29t05:10:41.208z", sender_username: "siehyvar", sender_uuid: "1294553"} 

now trying push object

object {content: "hi", sender_uuid: "1294553", sender_username: "siehyvar", date: "2016-08-29t05:47:40.232z"} 

with following code

$scope.messages: []; scope.$on(api.getmessageeventnamefor(), function(ngevent, m) {     scope.$apply(function() {         $scope.messages.push(m);     }); }); 

and error getting

messages.push not function 

i suppose messages here array of objects .push function not working. can please help?

let me explain code. have 1 chat api putting pubnub code, 2 controllers(for online users , chat) , 2 twig templates(for online users , chat)

messages declaration current channel chatapi :

current_channel: {     channel_name: null,     channel_label: null,     messages: [] }, 

join chat event chatapi

joinchat: function (chat_channel,scope) {         var channel = chat_channel.channel;         var channel_name = chat_channel.name;         //join chatroom channel room name         api.chatrooms.addme(channel);          //get online users in chatroom         api.chatrooms.onlinemembers(channel, scope);         //api.current_channel.channel_id = channel;         api.current_channel.channel_name = channel;         api.current_channel.channel_label = channel_name;         console.log(api.current_channel.channel_name);         api.getpresenceeventnamefor();          // listening callbacks getting messages current channel         scope.$on(api.getmessageeventnamefor(), function(ngevent, m) {             scope.$apply(function() {                 if (angular.isarray(scope.current_channel.messages)) {                     scope.current_channel.messages.push(m);                 }else {                     console.log("not array");                 }             });             scroller.down(500);         });          api.gethistory(channel, scope); 

twig joining chat user

<ul class="all-chats-list" ng-repeat="onlineuser in online.users"> <li class="col-xs-12 nopadding" ng-click="joinchat(onlineuser.chat_channel); selectme($event);"> 

and messages being displayed

<li ng-repeat="message in current_channel.messages"> 

controller joining chat

$scope.joinchat = function(chat_channel) {      chatapi.joinchat(chat_channel, $scope); }; 

now when calling send message

twig:

<form ng-submit="sendmessage()">     <div class="col-xs-9 col-md-10 form-group nopadding">         <input style="width: 100%" ng-model="message.content"  class="form-control" type="text" placeholder="type message" />     </div>     <div class="col-xs-3 col-md-2 form-group nopadding">         <button type="submit" class="form-control btn site-btn" id="send-message">send</button>     </div> </form> 

chatapi:

sendmessage: function() {         console.log('publish to: ' + api.current_channel.channel_name);         // don't send empty message         if (!api.message.content ||             api.message.content === '') {             return;         }          pubnub.publish({             channel: api.current_channel.channel_name,             message: {                 content: api.message.content,                 sender_uuid: models.user.uuid,                 sender_username: models.user.username,                 date: new date()             },             callback: function(m) {                 //console.log(m)             }         });          // reset messagecontent input         api.message.content = '';      }, 

chat controller:

$scope.sendmessage = chatapi.sendmessage; 

i getting error messages.push not function. guess because treating object , not array.

hope clear now.

adding others have mentioned,

the obvious issue here: $scope.messages: [];

am getting error messages.push not function.

just replace declaration of $scope.messages with:

$scope.messages = [];

now able call push method on $scope.messages array.

in example $scope.messages of type object (it should've been of type array).

you can read more methods available array , object types in javascript here. reason why can't call push on object js is

in case want take tacky road , not define array, guest , create prototype method push object types. way can call push on objects. that'll silly though , you'd run in loads of issues when trying pop() or unshift() elements in object (that want treat array).


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -