invalid code generated

187 views
Skip to first unread message

Paul Davis

unread,
Feb 24, 2021, 6:53:01 AM2/24/21
to Blockly
hi ive created a custom blockly block and it functions with no issues but i am constantly getting spammed by this error: Uncaught SyntaxError: Invalid code generated: undefined
Does anyone know how to suppress this error? it is also causing issues where if i autogenerate some blocks they will not work until i have clicked and dragged the blocks before pressing play.
Here is the constructor code for the block: 

JSON:
{
  "type": "del_move_right",
  "message0": "Move Drone Right %1",
  "args0": [
    {
      "type": "input_value",
      "name": "right",
      "check": "Number"
    }
  ],
  "previousStatement": null,
  "nextStatement": null,
  "colour": '#D91E1E',
  "tooltip": "",
  "helpUrl": ""
}

JS CODE:
Blockly.JavaScript['del_move_right'] = function(block) {
  var value_name = Blockly.JavaScript.valueToCode(block, 'right', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = 'delMoveRight('+value_name+');\n';
  return code;
};

Neil Fraser

unread,
Feb 24, 2021, 1:21:53 PM2/24/21
to blo...@googlegroups.com
Everything looks absolutely correct to me.  Can you add a console.log(code); before the return?  My first instinct is that the presented code might not be the code that's actually running.  Logging the code would help validate that.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/a296f632-db28-4416-b2c4-833611f826e1n%40googlegroups.com.


--

Paul Davis

unread,
Feb 25, 2021, 5:36:35 AM2/25/21
to blo...@googlegroups.com
hi,

the console log returns: "delMoveRight();"

as i mentioned, theres nothing wrong with the code, it runs without issue but there is a big error in the console every time this block and other similar blocks 

below is the code i am using to interpret the blockly code:

function initEducationDelMoveRight(interpreter, globalObject)
{
var wrapper = async function(distance, callback)
{
                await moveRight(distance)
                callback();
};

Blockly.JavaScript.addReservedWords("delMoveRight");
interpreter.setProperty(globalObject, "delMoveRight", interpreter.createAsyncFunction(wrapper));
}

initEducationDelMoveRight(interpreter, globalObject);

You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/9rIjxRfbN9Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/CAEx9HWEU8boh4i8T%3DoTtK7_dvCT51%3DDE_q1zFcW-0AvTpLyBwA%40mail.gmail.com.

Beka Westberg

unread,
Feb 25, 2021, 4:33:15 PM2/25/21
to blo...@googlegroups.com
Hello,

It seems like the issue might be that your `delMoveRight` function expects a number as an argument, and it's not getting one because there's no block attached. You should be able to fix this by doing something like the following:

```
Blockly.JavaScript['del_move_right'] = function(block) {
  // This line changed (end of line)
  var value_right = Blockly.JavaScript.valueToCode(block, 'right', Blockly.JavaScript.ORDER_ATOMIC) || '0';
  var code = 'delMoveRight('+value_right+');\n';
  return code;
};
```

I hope that helps!
--Beka

Paul Davis

unread,
Feb 26, 2021, 5:41:08 AM2/26/21
to blo...@googlegroups.com
Hi Beka,

this was something i originally thought was wrong as well but unfortunately the same issue persists when i add in the " || '0' ". Thank you for your suggestion though!

Paul

Beka Westberg

unread,
Feb 26, 2021, 2:54:46 PM2/26/21
to blo...@googlegroups.com
Hello again,

So I did some looking, and I think  the error is getting thrown from here. It looks like that  error only gets thrown if you have a JavaScript generator defined, but it doesn't return a value (or the return statement isn't being reached). Is there any other block you think could be triggering this besides the del_move_right block? Since it seems like the generator for that is properly defined and getting called.

Otherwise I'm really not sure what's happening here :/
--Beka

Paul Davis

unread,
Feb 26, 2021, 2:57:07 PM2/26/21
to blo...@googlegroups.com
Hi beka, 

Thanks for looking into this further, you might be right about another block not returning something so I will investigate this further and get back to you. Thanks again!

Paul

Reply all
Reply to author
Forward
0 new messages