Help a newbie out!
I posted this as a question to an old topic but got no answer yet, so I thought why not make it a topic. I have looked and looked but haven’t found a tutorial or an example of actually reading data from an RSS feed to the client.
I’m using parse-feed to parse an RSS feed on the server side and that works well. However, being quite new to Meteor and Javascript in general, I can’t figure out how to display the feed in the client.
What I’m trying to accomplish is to display my latest blog posts from Medium on my personal website (under development). It would be easy with a normal pub/sub, but I don’t want to insert the posts to Mongo since they won’t be needed when newer posts come and replace them. This wouldn’t be a problem since the overall number of posts won’t ever be huge, but there has to be another way.
What I’ve tried is having a server side method that fetches and parses the feed:
fetchLatestBlogposts() {
let feed = require("feed-read");
let url = "https://www.medium.com/feed/[myusernamehere]";
let firstPosts = [];
feed(url, function(err, posts) {
if (err) {
console.log("There has been an error: " + err);
}
for(let i = 0; i < 2; i++) {
firstPosts.push({
"title": posts[i].title,
"content": posts[i].content,
"published": posts[i].published,
"link": posts[i].link
});
}
console.log("hello from server" +firstPosts);
return firstPosts;
});
}
This method successfully fetches the blogposts and inserts them to the array (which the server side console.log proves) I would like to return. I can’t however get them through to the client.
My client side looks somewhat like this. I’m using React as the UI component, so there are eg no Blaze templates.
posts() {
Meteor.call("fetchLatestBlogposts", function(err, result) {
if (err) {
console.log("error in client: " + err);
}
if (result) {
return result;
}
console.log("no result found");
});
}
render() {
return(
<div className="blog-latest">
<div className="blog-latest-text">
//irrelevant code until next line
{console.log("UI " + this.posts())}
</div>
</div>
)
}
The client logs “UI undefined” (from render()) and “no results found” (from posts()) respectively. The server side method does trigger and logs what it’s supposed to log to the server console. So it seems apparent that the render() finishes before posts(), but I can’t figure out why posts() doesn’t get any result from the callback.
I suppose my problem is rather with not quite understanding the asynchronous way that Javascript works than with Meteor, but any help is greatly appreciated.