.push不适用于angularjs和pubnub

时间:2016-08-29 06:01:00

标签: angularjs pubnub

我正在进行pubnub聊天。并试图将新消息推送到消息数组。 我有一组消息对象,如

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}

每个对象都包含

等数据
Object {content: "asd", date: "2016-08-29T05:10:41.208Z", sender_username: "siehyvar", sender_uuid: "1294553"}

现在我正在尝试推送另一个对象

Object {content: "hi", sender_uuid: "1294553", sender_username: "siehyvar", date: "2016-08-29T05:47:40.232Z"}

使用以下代码

$scope.messages: [];
scope.$on(API.getMessageEventNameFor(), function(ngEvent, m) {
    scope.$apply(function() {
        $scope.messages.push(m);
    });
});

我得到的错误是

messages.push is not a function

我认为这里的消息是一个对象数组,但.push函数不起作用。有人可以帮忙吗?

让我解释一下代码。所以我有一个聊天Api,我把所有的pubnub代码,两个控制器(用于在线用户和聊天)和两个twig模板(用于在线用户和聊天)

当前频道ChatApi的消息声明:

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

加入chatApi的聊天事件

joinChat: function (chat_channel,scope) {
        var channel = chat_channel.channel;
        var channel_name = chat_channel.name;
        //Join chatroom Channel with the 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 to the callbacks for getting messages for 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 is array");
                }
            });
            scroller.down(500);
        });

        API.getHistory(channel, scope);

加入与用户聊天的Twig

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

并且消息显示为

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

加入聊天的控制器

$scope.joinChat = function(chat_channel) {

    chatApi.joinChat(chat_channel, $scope);
};

现在我正在调用发送消息

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 your 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 an 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 the messageContent input
        API.message.content = '';

    },

聊天控制器:

$scope.sendMessage = chatApi.sendMessage;

我收到错误消息.push不是函数。 我想这是因为它将它视为一个对象,而不是一个数组。

希望现在很清楚。

2 个答案:

答案 0 :(得分:1)

添加其他人提到的内容,

显而易见的问题是: $scope.messages: [];

  

我收到错误消息.push不是一个函数。

只需将$ scope.messages的声明替换为:

$scope.messages = [];

现在,您可以在push数组上调用$scope.messages方法。

在您的示例中,$ scope.messages的类型为object(它应该属于array类型。)

您可以在javascript here中详细了解数组和对象类型可用的方法。 您之所以无法调用JS对象的推送是

如果您想采取俗气的道路并且不定义数组,那么请成为我的客人并为对象类型创建原型方法push。这样你就可以在对象上调用push。这会非常愚蠢,而且你会遇到许多问题,例如在你的对象中尝试pop()unshift()元素时(你想要像对待数组一样)。

答案 1 :(得分:0)

更换专栏

data.table::frank(df$Plotn, ties.method = "dense")
# [1] 1 1 1 1 2 3 3 3 4 4

通过

$scope.messages: [];

可以解决您的问题。