# My asynchronously received data is not published

**URL:** <https://discourse.nodered.org/t/my-asynchronously-received-data-is-not-published/83583>\
**Category:** Developing Nodes\
**Created:** [10 December 2023 19:18 UTC](https://discourse.nodered.org/t/my-asynchronously-received-data-is-not-published/83583 "2023-12-10T19:18:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![smudi](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@smudi](https://discourse.nodered.org/u/smudi)\
**Post date:** [10 December 2023 19:18 UTC](https://discourse.nodered.org/t/my-asynchronously-received-data-is-not-published/83583/1 "2023-12-10T19:18:04Z")

</div>

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:

```js
       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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 December 2023 19:40 UTC](https://discourse.nodered.org/t/my-asynchronously-received-data-is-not-published/83583/2 "2023-12-10T19:40:16Z")

</div>

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

```auto
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.

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

```

Then:

```auto
       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

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [10 December 2023 19:41 UTC](https://discourse.nodered.org/t/my-asynchronously-received-data-is-not-published/83583/3 "2023-12-10T19:41:37Z")

</div>

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:

```js
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?

---

<div class="post-metadata">

**Author:** ![smudi](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@smudi](https://discourse.nodered.org/u/smudi)\
**Post date:** [11 December 2023 10:57 UTC](https://discourse.nodered.org/t/my-asynchronously-received-data-is-not-published/83583/4 "2023-12-11T10:57:01Z")

</div>

> [@GogoVega](#):
>
> `msg.payload = {};`

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

But the other tips also helped 😉

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 December 2023 16:00 UTC](https://discourse.nodered.org/t/my-asynchronously-received-data-is-not-published/83583/5 "2023-12-11T16:00:12Z")

</div>

> [@GogoVega](#):
>
> I find it strange that node.log is correct, are you sure of that?

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:

- node.id : the id of the Function node - _added in 0.19_
- node.name : the name of the Function node - _added in 0.19_
- node.outputCount : number of outputs set for Function node _- added in 1.3_
- node.path: The flow/node ids
- node.log(..) : [log a message](https://nodered.org/docs/user-guide/writing-functions#logging-events)
- node.warn(..) : [log a warning message](https://nodered.org/docs/user-guide/writing-functions#logging-events)
- node.error(..) : [log an error message](https://nodered.org/docs/user-guide/writing-functions#logging-events)
- node.debug(..) : [log a debug message](https://nodered.org/docs/user-guide/writing-functions#logging-events)
- node.trace(..) : [log a trace message](https://nodered.org/docs/user-guide/writing-functions#logging-events)
- node.on(..) : [register an event handler](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)
- node.status(..) : [update the node status](https://nodered.org/docs/user-guide/writing-functions#adding-status)
- node.send(..) : [send a message](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)
- node.done(..) : [finish with a message](https://nodered.org/docs/user-guide/writing-functions#finishing-with-a-message)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 December 2023 16:00 UTC](https://discourse.nodered.org/t/my-asynchronously-received-data-is-not-published/83583/6 "2023-12-25T16:00:55Z")

</div>

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