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.

    Switch URL based on value

    Scheduled Pinned Locked Moved Solved Troubleshooting
    10 Posts 3 Posters 905 Views 3 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.
    • M Offline
      MMRIZE @htilburgs
      last edited by

      @htilburgs
      Just for your reference

      const ip_a = "..."
      const ip_b = "..."
      
      const apiQuery = (index, url) => {
        if (!url) return Promise.reject({ index, reason: 'url is not defined' })
        return new Promise((resolve, reject) => {
          try {
            fetch(url)
              .then(response => response.json())
              .then(data => {
                resolve({ index, value: data })
              })
              .catch(error => {
                reject({ index, reason: error })
              })
          } catch (error) {
            reject({ index, reason: error })
          }
        })
      }
      
      const queries = []
      queries.push(apiQuery('A', ip_a))
      queries.push(apiQuery('B', ip_b))
      Promise.allSettled(queries).then((results) => {
        console.log('All jobs are done')
        results.values().forEach((result) => {
          if (result.status === 'fulfilled') {
            console.log(`Index: ${result.value.index}, Value:`, result.value.value)
          } else {
            console.log(`Index: ${result.reason.index}, Error:`, result.reason.reason)
          }
        })
      })
      
      1 Reply Last reply Reply Quote 0
      • S Do not disturb
        sdetweil @htilburgs
        last edited by

        @htilburgs hm… looks ok, I would use the developer window sources tab to debug the code and look at the variables… (mouse over name usage will show contents)

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        htilburgsH 1 Reply Last reply Reply Quote 0
        • htilburgsH Offline
          htilburgs @sdetweil
          last edited by

          @sdetweil
          In the console - sources it says:

          Uncaught SyntaxError: Unexpected token 'if'
          

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

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

            @htilburgs well, if the code breaks, then …

            so, look at that line and see what is missing from before it

            a quick way to find syntax errors

            in the module folder, run the pre-compiler to check for errors

            node -c modulename.js
            

            (or any js file)

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            htilburgsH 1 Reply Last reply Reply Quote 0
            • htilburgsH Offline
              htilburgs @sdetweil
              last edited by

              @sdetweil
              Meanwhile I was doing this, and I found the error (stupid me)

              	start: function () {
              		Log.info("Starting module: " + this.name);
              		requiresVersion: "2.9.0";	
              			
              		// Set locales
              
              		if (this.config.P1_IP != null) {
              			this.urlP1 = "http://" + this.config.P1_IP + "/api/v1/data/";
              		} else {
              			this.urlP1 = "https://dummyjson.com/c/7e24-36ab-48e0-a96d";
              		}
              
              		if (this.config.WM_IP != null) {
              			this.urlWM = "http://" + this.config.WM_IP + "/api/v1/data/";
              		} else {
              			this.urlWM = "https://dummyjson.com/c/704a-9a96-4845-bc72";
              		}
              //		this.urlP1 = "http://" + this.config.P1_IP + "/api/v1/data/";
              //		this.urlWM = "http://" + this.config.WM_IP + "/api/v1/data/";
              		
                  		this.MHW_P1 = [];	        // <-- Create empty MHW_P1 array
              		this.MHW_WM = [];		// <-- Create empty MHW_WM array
              		this.scheduleUpdate();       	// <-- When the module updates (see below)
              	},
              

              The line with "requiresVersion: “2.9.0”; " was ending with an “,” instead of “;”

              without the “if” statement, I don’t get a failure with a “,”
              So changed it now into “;” and everythins seems to work.
              I continue testing.

              @MMRIZE Thanks for the alternative way. I’m going to look into it.
              So with the “promise” statement, it looks like I can eleminate the node_helper.js

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

              S 2 Replies Last reply Reply Quote 0
              • S Do not disturb
                sdetweil @htilburgs
                last edited by sdetweil

                @htilburgs said in Switch URL based on value:

                The line with "requiresVersion: “2.9.0”; " was ending with an “,” instead of “;”

                requiresVersion should be outside the start function… (and then needs a trailing comma

                Sam

                How to add modules

                learning how to use browser developers window for css changes

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

                  @htilburgs said in Switch URL based on value:

                  So with the “promise” statement, it looks like I can eleminate the node_helper.js

                  correct, now that electron (and nodejs) have fetch built in(since node 18), you don’t NEED to use node_helper to do it

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  1 Reply Last reply Reply Quote 0
                  • S sdetweil has marked this topic as solved on
                  • 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