0

我正在開發Chrome擴展程序並嘗試使用Google Feed API將擴展程序中的圖像添加到我的網站RSS源中。 但其投擲的錯誤未捕獲TypeError:無法讀取未定義的屬性'0'(Google Feed API)

Uncaught TypeError: Cannot read property '0' of undefined 

這裏的腳本

function myGetElementsByClassName(selector) { 
 
    if (document.getElementsByClassName) { 
 
     return document.getElementsByClassName(selector); 
 
    } 
 

 
    var returnList = new Array(); 
 
    var nodes = document.getElementsByTagName('div'); 
 
    var max = nodes.length; 
 
    for (var i = 0; i < max; i++) { 
 
     if (nodes[i].className == selector) { 
 
      returnList[returnList.length] = nodes[i]; 
 
     } 
 
    } 
 
    return returnList; 
 
} 
 

 
var rssReader = { 
 
    containers : null, 
 

 
    // initialization function 
 
    init : function(selector) { 
 
     containers = myGetElementsByClassName(selector); 
 
     for(i=0;i<containers.length;i++){ 
 
      // getting necessary variables 
 
      var rssUrl = containers[i].getAttribute('rss_url'); 
 
      var num = containers[i].getAttribute('rss_num'); 
 
      var id = containers[i].getAttribute('id'); 
 

 
      // creating temp scripts which will help us to transform XML (RSS) to JSON 
 
      var url = encodeURIComponent(rssUrl); 
 
      var googUrl = 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num='+num+'&q='+url+'&callback=rssReader.parse&context='+id; 
 

 
      var script = document.createElement('script'); 
 
      script.setAttribute('type','text/javascript'); 
 
      script.setAttribute('charset','utf-8'); 
 
      script.setAttribute('src',googUrl); 
 
      containers[i].appendChild(script); 
 
     } 
 
    }, 
 

 
    // parsing of results by google 
 
    parse : function(context, data) { 
 
     var container = document.getElementById(context); 
 
     container.innerHTML = ''; 
 

 
     // creating list of elements 
 
     var mainList = document.createElement('ul'); 
 

 
     // also creating its childs (subitems) 
 
     var entries = data.feed.entries; 
 
     for (var i=0; i<entries.length; i++) { 
 
      var img = new Image(); 
 
      img.src = entries[i].mediaGroups[0].contents[0].url; 
 
      listItem.appendChild(img); 
 
      var listItem = document.createElement('li'); 
 
      var title = entries[i].title; 
 
      var contentSnippet = entries[i].contentSnippet; 
 
      var contentSnippetText = document.createTextNode(contentSnippet); 
 

 
      var link = document.createElement('a'); 
 
      link.setAttribute('href', entries[i].link); 
 
      link.setAttribute('target','_blank'); 
 
      var text = document.createTextNode(title); 
 
      link.appendChild(text); 
 

 
      // add link to list item 
 
      listItem.appendChild(link); 
 

 
      var desc = document.createElement('p'); 
 
      desc.appendChild(contentSnippetText); 
 

 
      // add description to list item 
 
      // listItem.appendChild(desc); 
 

 
      // adding list item to main list 
 
      mainList.appendChild(listItem); 
 
      
 
     } 
 
     container.appendChild(mainList); 
 
    } 
 
}; 
 

 
window.onload = function() { 
 
    rssReader.init('post_results'); 
 

 
}
 <div class="post_results" id="post_results1" rss_num="4" rss_url="http://www.pixlov.com/feed"> 
 
      <div class="loading_rss"> 
 
       <img alt="Loading..." src="https://www.script-tutorials.com/demos/82/images/loading.gif" /> 
 
      </div> 
 
     </div>

問題是這一行

img.src = entries[i].mediaGroups[0].contents[0].url; 

但日e奇怪的事情是,當我添加此網站的飼料鏈接http://mightydeals.com/feed然後它的作品!

+0

壕溝可能是timming問題。使用調試器進行驗證。 –

+0

該Feed中沒有'mediaGroups' ... – Cornwell

+0

那麼Feed如何從Mightydeals工作而沒有任何錯誤? –

回答

0

問題出在feed url中。供稿網址數據的響應不包含mediaGroups屬性。所以當你試圖從它得到索引0值時,它會拋出0索引錯誤。 要首先解決該問題,您需要檢查響應數據是否包含mediaGroups密鑰。 像 如果(條目[I] .mediaGroups & &條目[I] .mediaGroups.length> 0)

相關問題