MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    SOLVED Getting error using rss-parser

    Troubleshooting
    2
    4
    101
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      sdillard last edited by

      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 have

      var 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:

      1. including this line below the require lines (see above),
      2. including in the socketNotificationReceived method, and
      3. 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…

      S 1 Reply Last reply Reply Quote 0
      • S
        sdetweil @sdillard last edited by sdetweil

        @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. }

        Sam

        Create a working config
        How to add modules

        S 1 Reply Last reply Reply Quote 0
        • S
          sdillard @sdetweil last edited by

          @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…

          S 1 Reply Last reply Reply Quote 0
          • S
            sdetweil @sdillard last edited by

            @sdillard glad u got it

            Sam

            Create a working config
            How to add modules

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Enjoying MagicMirror? Please consider a donation!
            MagicMirror created by Michael Teeuw.
            Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
            This forum is using NodeBB as its core | Contributors
            Contact | Privacy Policy