2015-11-09 3 views
1

Я передаю объект "text":"Hey! my chat text here" в тег div в DOM Я использую API-интерфейс pubnub streaming. Моя проблема undefined показывает для каждого объекта, который не имеет "text":"Hey! my chat text here" при подключении к каналу, это происходит внутри <div id="textbox"></div>. Как избавиться от undefined?undefined objects

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> 
+0

Я Ass Я не единственный, но единственный, кто сказал это, но я полностью прочитал этот титул. – MortenMoulder

+0

Да, извините, что название исправлено. – linski

ответ

2

Ok, я установил этот вопрос, видимо, мне нужна if и else заявление

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

+0

Или 'if (m.text! == undefined) {document.getElementById ('textbox') ...}' – ste2425