简体   繁体   中英

Not Populating list in HTML with Javascript

I am learning Javascript. I am working on reading RSS feeds for a personal project. I am using 'RSS-parser' npm library to avoid CORS error. And also I am using Browserify bundler to make it work on the browser.

When I run this code on the terminal it gives me output without any issue. But when I try with the browser it prints nothing.

My knowledge about Asynchronous JS is limited but I am pretty sure it doesn't have errors in here as I added code to it without changing existing code.

let Parser = require('rss-parser');
let parser = new Parser();

let feed;
async () => {

  feed = await parser.parseURL('https://www.reddit.com/.rss');
  feedTheList();
};


// setTimeout(function() {
//   //your code to be executed after 1 second
//   feedTheList();

// }, 5000);


function feedTheList()
{
  document.body.innerHTML = "<h1>Total Feeds: " + feed.items.length + "</h1>";


    let u_list = document.getElementById("list")[0];

    feed.items.forEach(item => {



        var listItem = document.createElement("li");

        //Add the item text
        var newText = document.createTextNode(item.title);
        listItem.appendChild(newText);
        listItem.innerHTML =item.title;

        //Add listItem to the listElement
        u_list.appendChild(listItem);

    });
}

Here is my HTML code.

<body>

     <ul id="list"></ul>
     <script src="bundle.js"></script>
</body>

Any guidance is much appreciated.

document.getElementById() returns a single element, not a collection, so you don't need to index it. So this:

let u_list = document.getElementById("list")[0];

sets u_list to `undefined, and you should be getting errors later in the code. It should just be:

let u_list = document.getElementById("list");

Also, when you do:

listItem.innerHTML =item.title;

it will replace the text node that you appended on the previous line with this HTML. Either append the text node or assign to innerHTML (or more correctly, innerText ), you don't need to do both.

Looks like the async call is not being executed; You need to wrap it in an anonymous function call:

See the example here: https://www.npmjs.com/package/rss-parser

Essentially,

var feed; // change let to var, so feed can be used inside the function
// wrap the below into a function call 
(async () => {
  feed = await parser.parseURL('https://www.reddit.com/.rss');
  feedTheList();
})(); // the (); at the end executes the promise

Now it will execute and feed should have items.

CORS errors when making request

As noted in the documentation at https://www.npmjs.com/package/rss-parser , if you get CORS error on a resource, use a CORS proxy. I've updated their example to fit your code:

// Note: some RSS feeds can't be loaded in the browser due to CORS security.
// To get around this, you can use a proxy.
const CORS_PROXY = "https://cors-anywhere.herokuapp.com/"

let parser = new RSSParser();
(async () => {
    await parser.parseURL(CORS_PROXY + 'https://www.reddit.com/.rss', function(err, feed) {    
        feedTheList(feed);
    });
})();

function feedTheList(feed)
{
    // unchanged
}

One last thing: The line

document.body.innerHTML = "<h1>Total Feeds: " + feed.items.length + "</h1>"; 

Will remove all of the content of <body>

I suggest to look into how element.appendChild works, or just place the <h1> tag in your HTML and modify its innerHTML property instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM