0
votes

I am doing this for learning purpose. I have url to RSS feed that I'd like to work further. This feed contains too much information. I'm interested only in all "item", their "title", "description" and "pubDate". I am using "firebase deploy --only functions" and then checking url for deployment where I expect to see cleaned data. For some reason I am getting error in cloud functions logs: "TypeError: Cannot read property 'channel' of undefined at cleanUp (/user_code/lib/index.js:19:29)"

I tried this with another URL which surprisingly worked: https://www.theguardian.com/uk/london/rss

Here is the URL to RSS feed I want to use: https://polisen.se/aktuellt/rss/hela-landet/handelser-i-hela-landet/

Here is my cleanUp function:

function cleanUp(data) {

    const items = []
    const channel = data.rss.channel
    channel.item.forEach(element => {
        items.push({
            title: element.title,
            description: element.description,
            date: element.pubDate
        })
    });
    return items
}

I expect to see all items with children title, description and pubdate after deployment. Instead I get "Error: could not handle the request" and when I check my logs in google cloud functions I see:

"TypeError: Cannot read property 'channel' of undefined at cleanUp (/user_code/lib/index.js:19:29)"

1

1 Answers

0
votes

To do so I would recommend working with JSON objects. So what I did is to get the RSS feed and convert it to a JSON object. Then parse item by item and log the data you need.

I have did a little bit of coding my self. Follow the steps below to get an idea of my example and then modify the code according to your needs:

  1. Create a new Google Cloud Function
  2. Trigger: HTTP
  3. Runtime: Node.js 8
  4. Add dependencies in package.json:
{
  "name": "sample-http",
  "version": "0.0.1",
   "dependencies": {
    "rss-to-json": "^1.0.4"
  }
}
  1. In index.js replace the code with my GitHub code example.

I have tested the code my self and it logged 200 items using the link you provided. Basically I take the RSS and convert it to JSON object. Then I iterate through all the items and log each item's property.