PubNub channelGroup.forEach不是一个函数

时间:2017-03-04 18:00:28

标签: javascript jquery pubnub

我正在与PubNub合作建立一个安全的消息系统,而且我遇到了频道列表中的问题,这些问题导致了一个“功能错误”,而不是一个功能错误'。这是我的代码:

$(document).on("pageshow", "#vendorMessages", function(event, ui) {
    var channelGroup = String(window.vendorID);
    pubnub.subscribe({
        channelGroups: channelGroup
    })
    pubnub.channelGroups.listChannels({
            channelGroup: channelGroup
        }, 
        function (status, response) {
            console.log("listing push channel for device")
            response.channels.forEach( function (channel) {
                console.log(channel)
            })
            response.channels.forEach( function (channel) {
                var channelFormatted = String(channel).split("_");
                var channelMember = channelFormatted[1];
                var e = $("#channelResults");
                e.append("<li onClick='loadChannel("+channel+")>"+channelFormatted+"</li>");
                e.listview("refresh");
            })
        }
    )
})

它返回此错误:

Uncaught TypeError: channelGroups.forEach is not a function
    at _class.adaptSubscribeChange (https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.4.js:3392:22)
    at HTMLDivElement.<anonymous> (http://www.weddingindustryinsider.com/www/index.html:504:10)
    at HTMLDocument.dispatch (https://code.jquery.com/jquery-1.11.1.js:4641:9)
    at HTMLDocument.elemData.handle (https://code.jquery.com/jquery-1.11.1.js:4309:28)
    at Object.trigger (https://code.jquery.com/jquery-1.11.1.js:4550:12)
    at HTMLDivElement.<anonymous> (https://code.jquery.com/jquery-1.11.1.js:5260:17)
    at Function.each (https://code.jquery.com/jquery-1.11.1.js:383:23)
    at jQuery.fn.init.each (https://code.jquery.com/jquery-1.11.1.js:136:17)
    at jQuery.fn.init.trigger (https://code.jquery.com/jquery-1.11.1.js:5259:15)
    at $.(anonymous function).(anonymous function)._triggerWithDeprecated (https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js:5020:29)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我有类似的问题。 问题是channel或channelGroups参数应该是一个数组。

替换:

var channelGroup = String(window.vendorID);

人:

var channelGroup = [String(window.vendorID)];