Read the statement by Michael Teeuw here.
Getting error using rss-parser
-
I’m creating a newer RSS feed module that updates regularly and rotates between different feeds. I think I am all good, but I am using rss-parser, and it doesn’t seem to be creating a new object when invoked.
At the top of my node_helper.js file, I havevar NodeHelper = require("node_helper"); var RssParser = require("rss-parser");
I’ve tried three ways to create a new parser object, but I always get parser.parse is undefined, which tells me that it isn’t creating the object, or the reference is invalid for some reason. I need a line to create the parser, like this:
var parser = new RssParser();
Here’s what I’ve tried:
- including this line below the require lines (see above),
- including in the socketNotificationReceived method, and
- creating a getter (see below)
getParser: function() { if (this.parser === undefined) { this.parser = new RssParser(); } return this.parser; }
No luck! The documentation for rss-parser is clear and it works great when I run a local node script that just reads a feed.
Help appreciated – I feel like I am missing something obvious…
-
@sdillard all variables defined inside the module register in node_helper need to be prefixed with this.
and if u are using them after definition inside a callback or .then() handler u need to be careful as this. might point someplace other than the module context…
for example a button press handler this. is pointing to the button object
the problem can be addressed by saving this to another local variable and using that local variable as the prefix inside the callback
let self=this
axios.get(…).then(function(parms){ self. }or using the es6 arrow function, which retains the this context
axios.get(…).then((parms)=>{ this. } -
@sdetweil Thanks for the response. I figured it out (just needed a fresh look, I guess). It didn’t have to do with scoping or binding – I was using parser.parse() when it should be parser.parseURL().
Just more for breadcrumbs for anyone reading this later, while it is true that it is important to scope a variable to self and do as you suggest, it is actually completely unnecessary in some cases and just leads to confusing code. If in my method I declare a require and then use that variable and no other method ever needs to use that variable, then scoping it to self just increases the size of the object unnecessarily since it can’t be garbage collected. And if I declare a require statement at the top of my module, I am creating a global variable, which can be dangerous and bad if I don’t understand what that means but is the right way if it is not an instance variable of the object.
I don’t want to seem unappreciative – I do really appreciate your saying this as it made me requestion my code…
-
@sdillard glad u got it