Dont use exec. There are helper functions built into node-red function node. These are immediately obvious if you use the monaco editor...
On to your issue: here is a function that works...
const pl = msg.payload
msg.payload = {}
// hard coded tests ↓ delete these!
getVar('global', 'global1', msg, 'payload.hard_coded.dest_sub_prop.global1_value')
getVar('flow', 'flow1', msg, 'payload.hard_coded.dest_sub_prop.flow1_value')
//dynamic from msg props
getVar(pl.scope, pl.prop, msg, pl.msgProp)
function getVar(scope, var_name, msg, msgProp) {
const contextscope = this[scope]
if (!contextscope) {
node.error(`bad scope: ${scope}`, msg);
return null
}
const value = RED.util.cloneMessage(contextscope.get(var_name))
if (msg) {
msgProp = msgProp || var_name
RED.util.setObjectProperty(msg, msgProp, value, true);
}
return value
}
return msg;
Here is a working demo demonstrating a few of the possibilities when using the node-red helpers...
[{"id":"588b281f44575274","type":"function","z":"255506af0d5ad011","name":"","func":"\nconst pl = msg.payload\nmsg.payload = {}\n\ngetVar('global', 'global1', msg, 'payload.hard_coded.dest_sub_prop.global1_value')\ngetVar('flow', 'flow1', msg, 'payload.hard_coded.dest_sub_prop.flow1_value')\n\n//dynamic from msg props\ngetVar(pl.scope, pl.prop, msg, pl.msgProp)\n\nfunction getVar(scope, var_name, msg, msgProp) {\n const contextscope = this[scope]\n \n if (!contextscope) {\n node.error(`bad scope: ${scope}`, msg);\n return null\n }\n const value = RED.util.cloneMessage(contextscope.get(var_name))\n if (msg) {\n msgProp = msgProp || var_name\n RED.util.setObjectProperty(msg, msgProp, value, true);\n }\n return value\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2280,"y":180,"wires":[["572e69379703fb44"]]},{"id":"964f2851a990da4b","type":"inject","z":"255506af0d5ad011","name":"global2 -> payload.value.of.global2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"scope\":\"global\",\"prop\":\"global2\",\"msgProp\":\"payload.value.of.global2\"}","payloadType":"json","x":2200,"y":120,"wires":[["588b281f44575274"]]},{"id":"572e69379703fb44","type":"debug","z":"255506af0d5ad011","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2410,"y":180,"wires":[]},{"id":"e3be80bc6dae8557","type":"inject","z":"255506af0d5ad011","name":"↑ set up context for tests ↑","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"scope\":\"global\",\"prop\":\"global3\",\"msgProp\":\"payload.value.of.global3\"}","payloadType":"json","x":2270,"y":260,"wires":[["4d80ecb1d6bdc85d"]]},{"id":"4d80ecb1d6bdc85d","type":"change","z":"255506af0d5ad011","name":"","rules":[{"t":"set","p":"flow1","pt":"flow","to":"one","tot":"str"},{"t":"set","p":"global1","pt":"global","to":"1","tot":"str"},{"t":"set","p":"global2","pt":"global","to":"two","tot":"str"},{"t":"set","p":"globalObject","pt":"global","to":"{\"nest1\":{\"nest2\":[0,1,2,3,4,5]}}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":2320,"y":300,"wires":[[]]},{"id":"ceb8f659ec1392a4","type":"inject","z":"255506af0d5ad011","name":"globalObject.nest1 -> payload.globalObject_nest1_here","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"scope\":\"global\",\"prop\":\"globalObject.nest1\",\"msgProp\":\"payload.globalObject_nest1_here\"}","payloadType":"json","x":2260,"y":80,"wires":[["588b281f44575274"]]},{"id":"e035293bb7f1d77b","type":"inject","z":"255506af0d5ad011","name":"bad scope!","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"scope\":\"blah\",\"prop\":\"global3\",\"msgProp\":\"payload.value.of.global3\"}","payloadType":"json","x":2120,"y":180,"wires":[["588b281f44575274"]]}]