MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Stuck trying to sendSocketNotification from my node_helper

    Scheduled Pinned Locked Moved Troubleshooting
    4 Posts 2 Posters 644 Views 2 Watching
    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 Offline
      seeshaughnessy
      last edited by

      I must be doing missing something obvious, but I’ve been at this forever. Sorry if the code looks rough, but I’m new to coding and still have some learning to do…

      I was able to get my requests just fine when I used axios and async/await (I’m most familiar with this library). I’m trying to convert the code to use the request library that’s built into Magic Mirror, but I can’t figure out what I’m doing wrong.

      I’m able to get the array I need with the request.get, but none of the code after my console.log('Result: ', result); // check seems to be working. I can’t log my filteredParcels, and the notification is not being sent.

      Am I missing something silly? Or is my whole concept incorrect?

      getOneTracker: function () {
          // Authenticate and get token
          var options = {
            uri: 'https://api.onetracker.app/auth/token',
            method: 'POST',
            json: {
              email: this.config.username,
              password: this.config.password,
            },
          };
      
          request(options, function (error, response, body) {
            if (!error && response.statusCode == 200) {
              const authToken = body.session.token;
      
              const options = {
                uri: 'https://api.onetracker.app/parcels',
                json: true,
                headers: {
                  'x-api-token': authToken,
                },
              };
      
              request.get(options, function (error, response, body) {
                if (!error && response.statusCode == 200) {
                  let result = body.parcels;
                  console.log('Result: ', result); // check
      
                  const filteredParcels = result.filter((parcel) => {
                    this.getDaysToReceive(parcel);
                  });
      
                  this.sendSocketNotification('ONETRACKER_RESULT', filteredParcels);
      
                }
              });
            }
          });
      
      
        },
      
        // Returns days left until delivery, null if delivered 1+ days ago, and ? if delivery is unknown
        getDaysToReceive: function (parcel) {
          const parcelStatus = parcel.tracking_status;
          const parcelDate = parcel.tracking_time_estimated;
          const parcelDay = parcelDate.substr(8, 2); //Get day from tracking data
          var today = new Date().toString().substr(8, 2); //Get todays date
          const daysToDelivery = parcelDay - today;
      
          if (parcelStatus != 'delivered' && daysToDelivery < 0) return '?';
          if (parcelStatus != 'delivered' && daysToDelivery >= 0)
            return daysToDelivery;
          if (parcelStatus == 'delivered' && daysToDelivery == 0) return '0';
          return;
        },
      
      S 1 Reply Last reply Reply Quote 0
      • S Do not disturb
        sdetweil @seeshaughnessy
        last edited by

        @seeshaughnessy the problem is what the ‘this’ pointer points to

        inside the request callback, ‘this’ points to the request, NOT the node_helper data.

        this is why we assign ‘this’ to a variable ‘self’ BEFORE calling request, and then use self. inside the callback instead of this.

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        1 Reply Last reply Reply Quote 0
        • S Offline
          seeshaughnessy
          last edited by

          Darnit, that makes total sense! Thank you!

          S 1 Reply Last reply Reply Quote 0
          • S Do not disturb
            sdetweil @seeshaughnessy
            last edited by sdetweil

            @seeshaughnessy you can continue to use axios. preferred as request has been depreciated.

            just add it to your package.json to get it loaded w your module

            # create package.json
            npm init -y
            # install a library and record it
            npm install axios --save
            #
            

            in your module folder

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            1 Reply Last reply Reply Quote 0

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • 1 / 1
            • First post
              Last post
            Enjoying MagicMirror? Please consider a donation!
            MagicMirror created by Michael Teeuw.
            Forum managed by Sam, technical setup by Karsten.
            This forum is using NodeBB as its core | Contributors
            Contact | Privacy Policy