How to : Retrieve RSS items in WIX Code

I thought I would share some code…

In the backend/serviceModule.jsw I added the below function. I added an indexNr because I only want to get the last item from the RSS Feed when I load my page.

export function getRSSFeed(indexNr) {
const url = “https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Fwww.sn.se%2Ffeed%2F”;

return fetch(url, {method: ‘get’})
.then(response => response.json())
.then(json => json.items[indexNr]);
}

Then in my page code I have this code…

import wixData from ‘wix-data’;
import {getRSSFeed} from ‘backend/serviceModule’;

$w.onReady(function () {
getRSSFeed(0)
.then(news => {
// does this records exists in the dataset?
wixData.query(“news”)
.eq(“title”, news.title)
.limit(1)
.find()
.then( (results) => {

 if (results.totalCount === 1) 
 { 
 	console.log(results.items); // it exists already 
 } else if (results.totalCount === 0) { // it does not exist 
 	let toInsert = { 
		"title":        news.title, 
		"newsImage":   	news.enclosure["link"], 
		"categoryName":		news.categories[0], 
		"datePublished":	news.pubDate, 
		"sourceUrl":		news.link 
	}; 
	console.log("created object for insertion"); 
	let options = { 
		"suppressAuth": true, 
		"suppressHooks": true 
	}; 

	wixData.insert("news", toInsert, options) 
		.then( (results) => { 
		let item = results; //see item below 
			console.log("saved new item in dataset news"); 
			$w("#newsdataset").refresh(); // refresh dataset on page 
		} ) 
		.catch( (err) => { 
			let errorMsg = err; 
		}); 
 	} 
}).catch( (err) => { 
  console.log("error " + err.code); 
}); 

}); 

} );

The code above will query any rss feed you setup through the rss2json.com service. It will then get the latest record in that RSS Feed and match it to the records in the news dataset. If it does not find a record with that title it will insert it into the database. If it exists it will console.log that items result.

This way you can read and get RSS Feeds into your WIX Sites and use external data in sp many ways.

Happy coding!

Hi Andreas,

thank you for sharing with the community!

2 Likes

Hi Andreas -
nice work!

can you share a bit of feedback on the user experience so far?
likes and dislikes?
thanks!

Thanks for providing this - I got this error in the console:
Uncaught (in promise) ReferenceError: fetch is not defined
and realized I needed this: import {fetch} from ‘wix-fetch’;