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

Uncaught SyntaxError: Unexpected token return



  • 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;		
    	
    	}, // 


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



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



  • use a variable to hold the value

    var x = 0

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

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



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



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


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



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



  • @sdetweil Oké, thank you for the explanation.


Log in to reply