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

Uncaught SyntaxError: Unexpected token return

Scheduled Pinned Locked Moved Solved Troubleshooting
9 Posts 2 Posters 2.4k 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 htilburgs Mar 22, 2019, 3:50 PM Mar 22, 2019, 3:46 PM

    I’m creating a new module. I’m testing and get the error “Uncaught SyntaxError: Unexpected token return” at the line where is

    return table;
    

    This is the part where it goes wrong.
    I’ve been looking for typos and other errors, but cannot find them.
    Has anybody a clue?

    
    	getDom: function() {
    		
    		// creating the table
    		var table = document.createElement("table");
    		table.className = "small";
    		
    		// creating the wrapper
    		var wrapper = document.createElement("div");
    		wrapper.className = "wrapper";
    		wrapper.style.maxWidth = this.config.maxWidth;
    	
    		// The loading sequence
            	if (!this.loaded) {
                	    wrapper.innerHTML = "Loading....";
                	    wrapper.classList.add("bright", "light", "small");
                	    return wrapper;
            	}	
    		
    		var MWB = this.MWB;
    
    		// creating the tablerows
    		var WoonplaatsRow = document.createElement("tr")
    		WoonplaatsRow.className = "woonplaats-row";
    		
    		var WoonplaatsCell = document.createElement("td");
    		WoonplaatsCell.className = "small";
    		WoonplaatsCell.innerHTML = "Regel1";
    		WoonplaatsRow.appendChild(WoonplaatsCell);
    		table.appendChild(WoonplaatsRow);
    		
    		var TemperatuurRow = document.createElement("tr")
    		TemperatuurRow.className = "temperatuur-row";
    		
    		var TemperatuurCell = document.createElement("td");
    		TemperatuurCell.className = "small";
    		TemperatuurCell.innerHTML = "Regel2"
    		TemperatuurRow.appendChild(TemperatuurCell);
    		table.appendChild(TemperatuurRow);
    					
    		}		
    		return table;		
    	
    	}, // <-- closes the getDom function from above```

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

    S 1 Reply Last reply Mar 22, 2019, 5:50 PM Reply Quote 0
    • S Offline
      sdetweil @htilburgs
      last edited by Mar 22, 2019, 5:50 PM

      @htilburgs said in Uncaught SyntaxError: Unexpected token return:

        }		
        return table;		
      

      u have an extra close brace, which matches up with the getDom() {
      and so, that return is just hanging out there

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      1 Reply Last reply Reply Quote 0
      • H Offline
        htilburgs
        last edited by Mar 23, 2019, 10:14 AM

        @sdetweil , thanx, missed that.
        Now that this part is running the JSON data is fetched. But when I trying to show the data I get an error:

        Uncaught SyntaxError: Unexpected number
        

        This error is created by the zero in the line:
        WoonplaatsCell.innerHTML = MWB.0.plaats;

        		var MWB = this.MWB;
        
        		// creating the tablerows
        		var WoonplaatsRow = document.createElement("tr");
        		WoonplaatsRow.className = "woonplaats-row";
        		
        		var WoonplaatsCell = document.createElement("td");
        		WoonplaatsCell.className = "small";
        		WoonplaatsCell.innerHTML = MWB.0.plaats;
        		WoonplaatsRow.appendChild(WoonplaatsCell);
        		table.appendChild(WoonplaatsRow);
        

        The problem seems that I cannot use the 0 in the command.

        This is the JSON result

        object{1}
        	liveweer[1]
        		0{49}
        			alarm: "0"
        			d0neerslag: "4"
        			d0tmax: "12"
        			plaats: "Amsterdam"
        			...
        

        I used a JSON checker and it says that it’s valid JSON.
        How do I display the data from this result, if I cannot use the 0?

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

        1 Reply Last reply Reply Quote 0
        • S Offline
          sdetweil
          last edited by Mar 23, 2019, 12:13 PM

          use a variable to hold the value

          var x = 0

          WoonplaatsCell.innerHTML = MWB.x.plaats;
          or use a literal

          WoonplaatsCell.innerHTML = MWB.[‘0’].plaats;

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          1 Reply Last reply Reply Quote 0
          • H Offline
            htilburgs
            last edited by Mar 23, 2019, 1:00 PM

            No luck…
            I’ve tried both of the options:

            	var MWB = this.MWB;
            	var n = 0;
            
            	// creating the tablerows
            	var WoonplaatsRow = document.createElement("tr");
            	WoonplaatsRow.className = "woonplaats-row";
            	
            	var WoonplaatsCell = document.createElement("td");
            	WoonplaatsCell.className = "small";
            	WoonplaatsCell.innerHTML = MWB.n.plaats;
            	WoonplaatsRow.appendChild(WoonplaatsCell);
            	table.appendChild(WoonplaatsRow);
            

            Error: Uncaught (in promise) TypeError: Cannot read property ‘plaats’ of undefined

            	var MWB = this.MWB;
            
            	// creating the tablerows
            	var WoonplaatsRow = document.createElement("tr");
            	WoonplaatsRow.className = "woonplaats-row";
            	
            	var WoonplaatsCell = document.createElement("td");
            	WoonplaatsCell.className = "small";
            	WoonplaatsCell.innerHTML = MWB.['0'].plaats;
            	WoonplaatsRow.appendChild(WoonplaatsCell);
            	table.appendChild(WoonplaatsRow);
            

            Error: Uncaught SyntaxError: Unexpected token [

            So both of the options give an error.

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

            S 1 Reply Last reply Mar 23, 2019, 1:26 PM Reply Quote 0
            • S Offline
              sdetweil @htilburgs
              last edited by Mar 23, 2019, 1:26 PM

              @htilburgs said in Uncaught SyntaxError: Unexpected token return:

              WoonplaatsCell.innerHTML = MWB.[‘0’].plaats;

              sorry, my bad, try these

              WoonplaatsCell.innerHTML = MWB[n].plaats;
              WoonplaatsCell.innerHTML = MWB['0'].plaats;
              

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              H 1 Reply Last reply Mar 23, 2019, 2:02 PM Reply Quote 0
              • H Offline
                htilburgs @sdetweil
                last edited by Mar 23, 2019, 2:02 PM

                @sdetweil YES, that did the trick! Learning a lot these few days.
                Thanks again.

                Is this notation always like this if you’re working with numbers?

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

                S 1 Reply Last reply Mar 23, 2019, 2:21 PM Reply Quote 0
                • S Offline
                  sdetweil @htilburgs
                  last edited by Mar 23, 2019, 2:21 PM

                  @htilburgs not so much numbers, but variable data… the notation

                  MWB.n.plaats
                  

                  means the plaats element of the n element of MWB
                  so the dot notation treats the between dot things as literals…

                  a number can’t be a variable name literal in this sense…
                  so, you need the bracket notation [] (and remove the left side dot when u use it this way)

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  H 1 Reply Last reply Mar 23, 2019, 4:13 PM Reply Quote 0
                  • H Offline
                    htilburgs @sdetweil
                    last edited by Mar 23, 2019, 4:13 PM

                    @sdetweil Oké, thank you for the explanation.

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

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    1 / 1
                    • First post
                      1/9
                      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