• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

Need help for rewrite node_helper.js from 'request' to 'got'

Scheduled Pinned Locked Moved Development
19 Posts 2 Posters 1.2k 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.
  • H Offline
    htilburgs
    last edited by Jun 20, 2024, 6:39 PM

    As we all know, the Nodejs module “request” is deprecated.
    I like to rewrite my node_helper.js module so request is not used anymore. Instead the Nodejs module “got” will be used.

    Module: https://github.com/htilburgs/MMM-MyPrayerTimes

    Can someone help me on my way?
    I’m not a developer and just do it for fun.

    (still trying to learn JS, but not afraid to ask) ☺

    S 1 Reply Last reply Jun 20, 2024, 7:37 PM Reply Quote 0
    • S Offline
      sdetweil @htilburgs
      last edited by Jun 20, 2024, 7:37 PM

      @htilburgs just use the built in fetch

      no libraries needed

      fetch is in node 18 or above and electron 26 or above.

      very similar to request.

      look at the calendar fetcher js, which used request before.

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      H 2 Replies Last reply Jun 24, 2024, 7:49 PM Reply Quote 0
      • H Offline
        htilburgs @sdetweil
        last edited by Jun 24, 2024, 7:49 PM

        @sdetweil

        Oké, made a start.
        Changed my node_helper.js to:

        /*
        //-------------------------------------------
        MMM-MyPrayerTimes
        Copyright (C) 2019 - H. Tilburgs
        MIT License
        //-------------------------------------------
        */
        
        const NodeHelper = require('node_helper');
        
        module.exports = NodeHelper.create({
        
          start: function() {
                  console.log("Starting node_helper for: " + this.name);
          },
        
         getMPT: function(url) {
                // Make a GET request using the Fetch API
                fetch(url)
                .then(response => {
                if (!response.ok) {
                  throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(result => {
            // Process the retrieved user data
            console.log(result);
            this.sendSocketNotification('MPT_RESULT', result);
          })
          .catch(error => {
            console.error('Error:', error);
          });
        },
        
        
          socketNotificationReceived: function(notification, payload) {
                    if (notification === 'GET_MPT') {
                    this.getMPT(payload);
                    }
          }
        });
        

        In the console I see the data:

        [2024-06-24 21:39:44.867] [LOG]   {
          code: 200,
          status: 'OK',
          data: {
            timings: {
              Fajr: '03:08',
              Sunrise: '05:21',
              Dhuhr: '13:40',
              Asr: '18:03',
              Sunset: '21:59',
              Maghrib: '21:59',
              Isha: '00:04',
              Imsak: '02:58',
              Midnight: '01:40',
              Firstthird: '00:26',
              Lastthird: '02:54'
            },
            date: {
              readable: '24 Jun 2024',
              timestamp: '1719257984',
              hijri: [Object],
              gregorian: [Object]
            },
            meta: {
              latitude: 51.48167,
              longitude: 5.66111,
              timezone: 'Europe/Amsterdam',
              method: [Object],
              latitudeAdjustmentMethod: 'ANGLE_BASED',
              midnightMode: 'STANDARD',
              school: 'STANDARD',
              offset: [Object]
            }
          }
        } 
        

        Only 1 problem, times are not displayed in de module

        484fd6c8-7285-4aef-ad68-9445b6a22fb2-image.png

        I know it has something to do with

        var result = JSON.parse(body).data.timings;               // 
        

        but I’m having troubles with this.
        When I replace body with result, I get an error.
        I’ve been looking for a solution for a few days now, but no result.

        Some help is appreciated ;-)

        (still trying to learn JS, but not afraid to ask) ☺

        1 Reply Last reply Reply Quote 0
        • H Offline
          htilburgs @sdetweil
          last edited by Jun 24, 2024, 8:08 PM

          @sdetweil

          I kept on searching and think I’ve found it.
          I didn’t use JSON.parse(), but simply put it in the result

              this.sendSocketNotification('MPT_RESULT', result.data.timings);
          

          It works, but I hope this is the right way.

          (still trying to learn JS, but not afraid to ask) ☺

          S 1 Reply Last reply Jun 24, 2024, 8:27 PM Reply Quote 0
          • S Offline
            sdetweil @htilburgs
            last edited by Jun 24, 2024, 8:27 PM

            @htilpburgs I was going to say the result of json() is passed already

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            H 1 Reply Last reply Jun 24, 2024, 8:37 PM Reply Quote 0
            • H Offline
              htilburgs @sdetweil
              last edited by Jun 24, 2024, 8:37 PM

              @sdetweil
              As far as I can see this works the correct way.
              Do you agree?

              (still trying to learn JS, but not afraid to ask) ☺

              S 1 Reply Last reply Jun 24, 2024, 8:57 PM Reply Quote 0
              • S Offline
                sdetweil @htilburgs
                last edited by sdetweil Jun 24, 2024, 8:58 PM Jun 24, 2024, 8:57 PM

                @htilburgs depends on what you mean by correct.

                it returns the parsed text as an object
                not text.

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                H S 2 Replies Last reply Jun 24, 2024, 9:24 PM Reply Quote 0
                • H Offline
                  htilburgs @sdetweil
                  last edited by Jun 24, 2024, 9:24 PM

                  @sdetweil
                  That I don’t understand.
                  Is this a problem? What would be better?

                  (still trying to learn JS, but not afraid to ask) ☺

                  S 1 Reply Last reply Jun 24, 2024, 10:07 PM Reply Quote 0
                  • S Offline
                    sdetweil @htilburgs
                    last edited by Jun 24, 2024, 10:07 PM

                    @htilburgs the response.json() function returns a js object already parsed.

                    thst is what yiy were going to fo w JSON.parse(). but don’t need to.

                    I don’t know if the CONTENT of the object is correct, but it should be for the same request

                    Sam

                    How to add modules

                    learning how to use browser developers window for css changes

                    H 1 Reply Last reply Jun 24, 2024, 10:14 PM Reply Quote 0
                    • H Offline
                      htilburgs @sdetweil
                      last edited by Jun 24, 2024, 10:14 PM

                      @sdetweil
                      In the previous version with ‘request’ I used JSON.parse() and got the same results.
                      The module works, but I didn’t know for sure if I used the correct approach.

                      (still trying to learn JS, but not afraid to ask) ☺

                      S 1 Reply Last reply Jun 24, 2024, 10:16 PM Reply Quote 0
                      • 1
                      • 2
                      • 1 / 2
                      1 / 2
                      • First post
                        1/19
                        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