My asynchronously received data is not published

Hi guys,

I am trying to develop a little node which is asynchronously calling an endpoint.

And in my node I have this function, where getStationStatus return a promise:

       node.on('input', function(msg, send, done) {

            send = send || function() { node.send.apply(node,arguments) }
            
            msg.payload = "data-before-call";
            
            const stationService = require('./services/station_service');
            stationService.getStationStatus(node.stationID).then(
                result => {

                    //msg.payload = result;
                    console.log(result.goalId);
                    name = result.name;
                    
                    msg.payload.goalId = result.goalId;
                    msg.payload.name = result.name;

                    send(msg);
                    node.log("msg sent");
                    node.log(msg);
                    done();
                },
                error => {
                    console.log(error);
                    msg.payload = error;
                    done(error);
                }
            );
        });

The service is called correctly and I also see the logs from node.log(msg); which have the correct data.
But if I add a debug Node after this node, the debug node only prints data-before-call as if send(msg) has no effect.

I don't know what's wrong here.
In my oppinion I am doing what is suggested in the documentation.

The easier way is to declare the input callback as async.

node.on('input', async function(msg, send, done) {
   ...
})

Then you can use await in the function. Everything should work then I think.

You can also remove the send line because Node-RED has the send callback now for several versions.

And I wouldn't force the input handler to deal with require for every message. Move that to a higher level so it is only processed once - preferably right at the start of your runtime code.

const {getStationStatus} = require('./services/station_service')

Then:

       node.on('input', async function(msg, send, done) {

            msg.payload = "data-before-call";

            try {            
                const stationService = await getStationStatus(node.stationID)
            } catch (err) {
                    console.log(error);
                    msg.payload = error;
                    done(error);
                    return
            } 

            console.log(result.goalId);
            name = result.name;

            // Oops - you made an assumption that msg.payload is an object
            // double-check that.
            msg.payload.goalId = result.goalId;
            msg.payload.name = result.name;

            send(msg);
            node.log("msg sent");
            node.log(msg);
            done();
     });

Untested

Hi,
Welcome to the forum.
The only thing I find wrong (strange) is your error function which is not in a catch but used as a second argument.

Like Julian said don't forget to add the following to ensure payload is an object:

msg.payload = {};
msg.payload.goalId = result.goalId;
msg.payload.name = result.name;

I find it strange that node.log is correct, are you sure of that?

Thank you guys!
The main problem was that I assumed payload is an object :see_no_evil:

But the other tips also helped :wink:

It is indeed a thing. It outputs direct to the Node-RED log (not to the debug panel). I always forget.

From my notes on the function node API's:

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