【问题标题】:PubNub history in Angular5Angular5 中的 PubNub 历史
【发布时间】:2018-04-17 13:28:14
【问题描述】:

我正在尝试将 pubnub 历史记录发布到我正在构建的网络应用程序中。我目前只能将其发送到控制台。这就是我所拥有的:

pubnub.history(
    {
        channel: 'jChannel',

        reverse: false, // false is the default
        count: 100, // 100 is the default
        stringifiedTimeToken: true // false is the default
    },
    function (status, response)
    {
        // LOG HISTORY TO CONSOLE
        console.log("History Below: ")
        console.log(response);
    }
);

有谁知道我如何将它发布到我的网络应用程序上? 我试过这样做:

{{response}}

在我的 html 中,但这不起作用。任何帮助表示赞赏。

我的 ts:

export class ArduinoAppComponent implements OnInit {
    public appStart = true;
    public myMessage;
    // CREATING MODEL FOR USER INPUT
    model = {
        firstname: "",
        lastname: "",
    }
    constructor(pubnub: PubNubAngular) {
        // LINKJ TO PUBNUB
        pubnub.init({
            publishKey: 'pub-',
            subscribeKey: 'sub-'
        })
        pubnub.subscribe({
            channels: ['jChannel'],
            triggerEvents: true,
            withPresence: true,

        })
        // CREATE PUBNUB HISTORY
        pubnub.history(
            {
                channel: 'jChannel',
                reverse: false, // false is the default
                count: 100, // 100 is the default
                stringifiedTimeToken: true // false is the default
            },
            function (status, response) {
                // LOG HISTORY TO CONSOLE
                console.log("History Below: ")
                console.log(response);
            }
        );

和html:

<div class="col-md-3 minus-margin2">
    <h2 style="padding-top: 75%;"> What's next?</h2>
    <button class="btn btn-info">next?</button>
    {{response}}
</div>

【问题讨论】:

标签: angular typescript angular5 pubnub


【解决方案1】:

您需要将历史响应的值存储到一个变量中。

export class ArduinoAppComponent implements OnInit {
public appStart = true;
public myMessage;
public history: any = {};
// CREATING MODEL FOR USER INPUT
 model = {
   firstname: "",
   lastname:"",
}


constructor(pubnub: PubNubAngular) {
  // LINKJ TO PUBNUB
  pubnub.init({
      publishKey: 'pub-',
      subscribeKey: 'sub-'
      })
      pubnub.subscribe({
        channels: ['jChannel'],
        triggerEvents: true,
        withPresence: true,

    })

    // CREATE PUBNUB HISTORY
    pubnub.history(
    {
        channel: 'jChannel',

        reverse: false, // false is the default
        count: 100, // 100 is the default
        stringifiedTimeToken: true // false is the default
    }, function (status, response){
        //store response in variable defined above.
        this.history = response;
    }
);

HTML

<div class="col-md-3 minus-margin2">
 <h2 style="padding-top: 75%;"> What's next?</h2>
 <button class="btn btn-info">next?</button>
   <span>{{history}}</span>
</div>

【讨论】:

  • 错误类型错误:无法分配给对象“[对象窗口]”的只读属性“历史”
  • ERROR TypeError: Cannot set property 'history' of undefined
  • 似乎响应是一个对象,您尝试使用 response.messages 获取消息并将其设置为历史变量。更多这里pubnub.com/docs/web-javascript/…
  • 所以当我这样做时,我得到了同样的错误,但是当我 console.log(response) 我得到这个:
  • {messages: Array(10), startTimeToken: "15238917484689756", endTimeToken: "15238917599944468"}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 1970-01-01
相关资源
最近更新 更多