Await and asynch errors within node-red node js file

Hi, I am attempting to expand the functionality of the node-red-serial-port node by allowing it to take in the PID, VID and serial number of a serial device and connect to the the corresponding serial port, as opposed to just entering the desired serial port.

To do this, I need to retrieve the list of serial devices connected to the device currently. To my knowledge this is done like this:

                        var ports_list = [];
                        await SerialPort.list().then(function(ports){
                          ports.forEach(function(port){
                           ports_list.push(port);
                          })
                        });

I have tested this in a function node and it appeared to work correctly. However, when I attempted to implement it into the node-red file I got this error:

15 Jul 08:55:30 - [info] Waiting for missing types to be registered:
15 Jul 08:55:30 - [info]  - serial-port (provided by npm module node-red-node-serialport)
15 Jul 08:55:30 - [info]  - serial in (provided by npm module node-red-node-serialport)
15 Jul 08:55:30 - [info] To install any of these missing modules, run:
15 Jul 08:55:30 - [info]   npm install <module name>
15 Jul 08:55:30 - [info] in the directory:

Although I am sure that the nodes are present as I do not get this error when I remove the added code.

I believe that it is an issue with the await function, as I do not get this error if I remove the await, however the list of serial devices cannot be collected.

Is there something I am missing in regards to using asynchronous functions in node-red?

Thanks

Hi @chappo_2

If there was an error whilst loading the node it would have been logged when Node-RED started. That would hopefully give a bit more of a clue as to what error you are hitting in the code.

Based on what you've shared, if the error only happens when you are using the await keyword, are you using it inside a function that is properly declared to be async?

Yep. I missed the actual error:

15 Jul 09:14:33 - [warn] [node-red-node-serialport/serialport] SyntaxError: await is only valid in async functions and the top level bodies of modules (line:343)

And yes. I was using it inside a function that wasnt decleared as async

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.