gpt4 book ai didi

javascript - undefined object

转载 作者:行者123 更新时间:2023-11-30 16:29:12 26 4
gpt4 key购买 nike

我正在传递一个对象 "text":"Hey! my chat text here"进入 DOM 中的 div 标签 我正在使用 pubnub 流 API。我的问题是 undefined为每个没有 "text":"Hey! my chat text here" 的对象显示当连接到 channel 时,这发生在 <div id="textbox"></div> 内部.我如何摆脱 undefined

undefined
undefined
undefined
Hey, my chat text
undefined
undefined
undefined
undefined

javascript代码

<script src="http://cdn.pubnub.com/pubnub-3.7.15.min.js"></script>
<script charset="utf-8">
PUBNUB.bind('load', window, function frontpageDemo() {

var PUBNUB_demo = PUBNUB.init({
subscribe_key: 'subkey_here',
});

PUBNUB_demo.subscribe({
'channel': "my_channel_here",
'connect': function(c) {
console.log('CONNECTED to ' + c);
},
'callback': function(m, a, subscribed_channel, c, real_channel) {
console.log(JSON.stringify(m));
console.log(JSON.stringify(subscribed_channel));
console.log(JSON.stringify(real_channel));
document.getElementById('textbox').innerHTML = '<div>' + m.text + '</div>' + document.getElementById('textbox').innerHTML;
}
})

});
</script>

最佳答案

好的,我解决了这个问题,显然我需要一个 ifelse 语句

PUBNUB_demo.subscribe({
'channel': "my_channel_here",
'connect': function(c) {
console.log('CONNECTED to ' + c);
},
'callback': function(m, a, subscribed_channel, c, real_channel) {
console.log(JSON.stringify(m));
console.log(JSON.stringify(subscribed_channel));
console.log(JSON.stringify(real_channel));
if (m.text === undefined) {}
else {document.getElementById('textbox').innerHTML = '<div>' + m.text + '</div>' + document.getElementById('textbox').innerHTML;
}
})

关于 undefined objects 的更多信息在 MDN

关于javascript - undefined object ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33601962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com