2015-11-09 1 views
1

Je passe un objet "text":"Hey! my chat text here" dans un tag div dans DOM J'utilise pubnub streaming API. Mon problème est undefined montre pour chaque objet qui n'a pas "text":"Hey! my chat text here" lorsqu'il est connecté à un canal cela se produit à l'intérieur <div id="textbox"></div>. Comment puis-je me débarrasser de undefined?undefined objects

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

code 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

I ass Je ne suis pas le seul, mais le seul à le dire, mais j'ai lu ce titre complètement faux. – MortenMoulder

+0

Ouais désolé à ce sujet, le titre corrigé. – linski

Répondre

2

Ok, je fixe la question apparemment je besoin d'une déclaration if et 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; 
} 
     }) 

Plus d'informations sur undefined objects à MDN

+0

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