javascript - PubNub channelGroup.forEach 不是函数

标签 javascript jquery pubnub

我正在与 PubNub 合作开发安全消息传递系统,但我遇到了 channel 列表返回“非函数错误”的问题。这是我的代码:

$(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)

任何帮助将不胜感激。

最佳答案

我也遇到了类似的问题。 问题在于 channel 或 channel 组参数应该是一个数组。

替换:

var channelGroup = String(window.vendorID);

作者:

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

关于javascript - PubNub channelGroup.forEach 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42599369/

相关文章:

javascript - 使用ajax从两个不同的while()循环获取多个数据

android - 如何停止在 PubNub 中接收我自己的消息

android - 后台应用程序在 Titanium 中不接收来自 pubnub for android 的消息

javascript - 如何使用 Moment.js 更改日期?

javascript - 如何制作填充视口(viewport) 50% 的内容 block ?

javascript - jquery图片无法预览

javascript - 关于 Underscore 源代码开头部分的混淆

javascript - 使用 jQuery 从 URL 弹出 iFrame

javascript - 多个链接的 getElementsByClassName 问题

C: 我能知道使用 C SDK 的 pubnub 订阅中的还原选项吗