phoddie / node-red-mcu

Node-RED for microcontrollers
124 stars 18 forks source link

Switch node crashing if property does not exist #109

Closed colinl closed 1 year ago

colinl commented 1 year ago

If I configure a Switch node to test, for example, global.mcuTopics.analogue_0.topic for not null, or not empty, or in fact anything else, I think, then if global.mcuTopics does not exist, the s/w crashes with code such as

        onMessage: function (msg) {
            let value = globalContext.get("mcuTopics").analogue_0.topic;
            const result = [null, ];
            if (this.empty(value)) {
                result[0] = msg;
            }
            return result;
        },

at the line let value = ... with the message "cannot coerce undefined to object!" The normal node-red does not crash, but instead then fails tests such as not empty and not null.

phoddie commented 1 year ago

Thanks for the report and example.

There's a straightforward fix for this in the code generator in nodered2mcu. That should be available later today from the Moddable SDK repository.

colinl commented 1 year ago

All working with the latest Modable SDK

phoddie commented 1 year ago

Great, thanks for the quick confirmation.

colinl commented 1 year ago

The ironical thing is, that because it didn't work, I had to find an alternative solution, and actually ended up with a significantly more efficient flow which didn't need the Switch node at all :-)

phoddie commented 1 year ago

Very nice. Constraints can necessitate a more thoughtful solution. That's part of what's fun about working on microcontrollers.