• Uncaught SyntaxError: BREAK statement outside of SWITCH, FOR or WHILE loop
    (But it's not.)
    On the left side:

    Post
    URL  {
      "method": "GET",
      "host": "",
      "path": "/CMD",
      "pathname": "/CMD",
      "search": null, "port": null, "query": null }
    XXX  /CMD
    Length  67
    pdata=  {"name":"Btemp","a":0,"cmd":"pobj.a=E.ge­tTemperature().toFixed(2)"}
    pdata length=  67
    {
      "name": "Btemp",
      "a": 0,
      "cmd": "pobj.a=E.getTemperature().toFixed(2)"
     }
    Uncaught SyntaxError: BREAK statement outside of SWITCH, FOR or WHILE loop
     at line 97 col 5
        break;
        ^
    Closed
    >
    
    

    The program continues running.
    Part of the code on the right side

       switch (a.pathname){
        case "/CMD":
          pobj=JSON.parse(pdata);
          console.log(pobj);
          if(pobj.cmd !==null){
           eval(pobj.cmd);
           pdata=JSON.stringify(pobj);
           res.writeHead(200);
           res.end(pdata);
          }else{
           res.writeHead(201);
           res.end();
          }//end if pobj.cmd
        break;    //this is the break being flaged on line 94
        case "/LOGdata":
         console.log("LOGdata= ",pdata);
         res.writeHead(200);            // this is the line reported 97
         res.end(pdata);
        break;
        default:
         res.writeHead(404);
         res.end();
        break;
      }//end switch
     pdata="";
    }//end doPost1
    
    

    Changing the code removes the error.

    function doPost1(req,res){
    var length;
    var a = url.parse(req.url, true);
    console.log("URL ",a);
    console.log("XXX ",a.pathname);
      length=req.headers["Content-Length"];
      console.log("Length ",length);
      console.log("pdata= ",pdata);
      console.log("pdata length= ",pdata.length);
      if(pdata.length!=length){
       res.writeHead(404);
       res.end();
       pdata="";
       return;
      }//end if length
       switch (a.pathname){
        case "/CMD":
         doCMD(req,res);
        break;
        case "/LOGdata":
         console.log("LOGdata= ",pdata);
         res.writeHead(200);
         res.end(pdata);
        break;
        default:
         res.writeHead(404);
         res.end();
        break;
      }//end switch
     pdata="";
    }//end doPost1
    
    function doCMD(req,res){
     pobj=JSON.parse(pdata);
     console.log(pobj);
     if(pobj.cmd !==null){
      eval(pobj.cmd);
      pdata=JSON.stringify(pobj);
      res.writeHead(200);
      res.end(pdata);
     }else{
      res.writeHead(201);
      res.end();
     }  //end if pobj.cmd
    }//end doCMD
    
    

    And on the left side:

    Get/
    Get/JPpostT9.html
    html
    End Pipe
    Uncaught Error: Expecting a function to call, got Number
    Post
    URL  {
      "method": "GET",
      "host": "",
      "path": "/CMD",
      "pathname": "/CMD",
      "search": null, "port": null, "query": null }
    XXX  /CMD
    Length  70
    pdata=  {"name":"LED1","a":0,"cmd":"pobj.a=!pobj­.a;digitalWrite(LED1,pobj.a)"}
    pdata length=  70
    {
      "name": "LED1",
      "a": 0,
      "cmd": "pobj.a=!pobj.a;digitalWrite(LED1,pobj.a­)"
     }
    Closed
    >
    
    
  • Why do you have a break after default?

  • Removing it doesn't fix it
    Old C++ habits linger.
    The left side:

    Post
    URL  {
      "method": "GET",
      "host": "",
      "path": "/CMD",
      "pathname": "/CMD",
      "search": null, "port": null, "query": null }
    XXX  /CMD
    Length  70
    pdata=  {"name":"LED1","a":0,"cmd":"pobj.a=!pobj­.a;digitalWrite(LED1,pobj.a)"}
    pdata length=  70
    {
      "name": "LED1",
      "a": 0,
      "cmd": "pobj.a=!pobj.a;digitalWrite(LED1,pobj.a­)"
     }
    Uncaught SyntaxError: BREAK statement outside of SWITCH, FOR or WHILE loop
     at line 93 col 5
        break;
        ^
    Closed
    >
    

    And the code with the default break commented

    function doPost1(req,res){
    var length;
    var a = url.parse(req.url, true);
    console.log("URL ",a);
    console.log("XXX ",a.pathname);
      length=req.headers["Content-Length"];
      console.log("Length ",length);
      console.log("pdata= ",pdata);
      console.log("pdata length= ",pdata.length);
      switch (a.pathname){
        case "/CMD":
         if(pdata.length==length){
          pobj=JSON.parse(pdata);
          console.log(pobj);
          if(pobj.cmd !==null){
           eval(pobj.cmd);
           pdata=JSON.stringify(pobj);
           res.writeHead(200);
           res.end(pdata);
          }else{
           res.writeHead(201);
           res.end();
          }  //end if pobj.cmd
         }//endif length else
        break;
        case "/LOGdata":
         console.log("LOGdata= ",pdata);
         res.writeHead(200);
         res.end(pdata);
        break;
        default:
         res.writeHead(404);
         res.end();
    //    break;  
     }//end switch
     pdata="";
    }//end doPost1
    
  • Ok, it looks like this is an Espruino issue caused by eval messing up the parse state.

    It can be caused just by:

    function test(){
     switch ("a"){
      case "a":
       eval(1);
       break;
     }
    }
    

    I've filed a bug for it here

    To work around this for now, add:

    function eval2(x) { return eval(x); }
    

    and then call eval2 instead of eval - hopefully that won't mess you up too much (although the new eval won't have access to the variables in the current scope).

  • Thanks for taking action

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Uncaught SyntaxError: BREAK statement outside of SWITCH, FOR or WHILE loop

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions