Node.js: Sending an empty array to maxAPI.outlet fails

Willis Morse's icon

TL;DR
Calling maxAPI.outlet([]) from a Node script returns "node.script: Node script tried to outlet an empty value". Is there a way to support returning empty arrays from Javascript?

Background
I am managing a dynamic array in Javascript using the Node.js/Max integration. Events from Max are sent to my Javascript function, my Javascript function manipulates its array according to some internal logic, and the Javascript function returns the resulting array back to Max using the maxAPI.outlet() function.

Some combination of Max events can legitimately result in an empty array, so I often need to be able to send an empty array back to Max in the maxAPI.outlet() function.

However, sending an empty array to maxAPI.outlet() results in no actual output in Max (ie - if I display the output in a Max message object, the previous array output is not replaced with nothing) and results in the following result in the Max console: "node.script: Node script tried to outlet an empty value"

Willis Morse's icon

A suboptimal workaround that I found is to send an empty string instead:

if (collector.notes.length === 0) {
maxAPI.outlet("");
}
else {
maxAPI.outlet(collector.notes);
}

Kind of gross, but it works I guess.

Still, I would be interested to hear what a correct approach would be.

tmhglnd's icon

You could prepend a "set" in the outlet function. Then outside [node.script] connect to the hot inlet of the message box. If the array is empty it will empty the message. Use the spread syntax (...) to place the contents of the array behind the "set" string, otherwise the object name will be returned.
maxApi.outlet("set", ...arr);

Alternatively you can check for the length with a conditional operator:
maxApi.outlet((arr.length == 0) ? "" : arr);