MagicMirror² v2.14.0 is available! For more information about this release, check out this topic.

Stuck trying to sendSocketNotification from my node_helper

  • 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: '',
          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: '',
              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.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';

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

  • Darnit, that makes total sense! Thank you!

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

Log in to reply