# Node.send causing error: this.\_flow is undefined

**URL:** <https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217>\
**Category:** Developing Nodes\
**Tags:** possible-bug\
**Created:** [24 March 2025 12:08 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217 "2025-03-24T12:08:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![papenfort](https://avatars.discourse-cdn.com/v4/letter/p/73ab20/32.png) [@papenfort](https://discourse.nodered.org/u/papenfort)\
**Post date:** [24 March 2025 12:08 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/1 "2025-03-24T12:08:32Z")

</div>

hey there, im trying to create a custom node that may send a message via a callback. for that purpose i have assigned the **this** from the constructor-function to the variable "node" like in the docs.

whenever i try to use node.send, i crash node-red becuase the the context doesnt contain the correct \_flow, i have also console.logged the this of the constructor for context:  
XYZConnectorConstructor {  
id: '40c25bbbcb943b6c',  
type: 'xyz-node',  
z: '96daf2ba776b0cab',  
g: undefined,  
\_closeCallbacks: ,  
\_inputCallback: [AsyncFunction (anonymous)],  
\_inputCallbacks: null,  
\_expectedDoneCount: 0,  
wires: [[ '8357e6c66d166b6b'], ['fbc2f2e3072130bc'] ],  
\_wireCount: 2,  
send: [Function (anonymous)]  
}

what possible causes could that have? id really like to use use node.send for doing some stuff.  
i do have node-red embedded for the option of adding custom middleware and stuff, but shouldnt change anything, right?

---

<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:** [24 March 2025 12:57 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/2 "2025-03-24T12:57:21Z")

</div>

It would help if you could share some code so that we can see the context.

The send function should be available once you have done `RED.nodes.createNode(this, config)` which is in the callback function to pass to `RED.nodes.registerType(mod.nodeName, nodeInstance)` (I always use a function called `nodeInstance`).

Example here: [RED.nodes.registerType(mod.nodeName, nodeInstance)](https://github.com/TotallyInformation/node-red-contrib-uibuilder/blob/b911de9e5759efe2aff4e401d0b9e5d85db0b3c1/nodes/uib-save/customNode.js)

If you are sending a msg in response to an incoming msg, then the `input` callback function contains its own `send` function.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 March 2025 13:06 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/3 "2025-03-24T13:06:17Z")

</div>

Are you using arrow functions?

Try using regular callbacks e.g. `function(x) { ... }` instead of `(x => ...)`

---

<div class="post-metadata">

**Author:** ![papenfort](https://avatars.discourse-cdn.com/v4/letter/p/73ab20/32.png) [@papenfort](https://discourse.nodered.org/u/papenfort)\
**Post date:** [24 March 2025 13:17 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/4 "2025-03-24T13:17:51Z")

</div>

my code basicially goes as follows:

```typescript
const nodeInit: NodeInitializer = (Red): void => {
  function BaseNodeNodeConstructor(
    this: Node,
    config: XYZ
  ): void {
    Red.nodes.createNode(
      this as Node<{}>,
      { ...config, outputs: 2 } as NodeDef
    );

    const callMap = new Map<string, Function>();
    const node = this

   
   

    this.on("input", async (msg, send, done) => {
      const input = msg as XYZMessage
      if (input.wlCallId) {
        callMap.get(input.wlCallId)?.(input);
        callMap.delete(input.wlCallId);
        done();
        return;
      }

   
      node.send([input, null]);

      const out = {
        ...input,
      };
    
      send([null, out]);
      done();
    });
  }

  Red.nodes.registerType("XYZ-definer-node", BaseNodeNodeConstructor, {
    outputs: 2,
  } as any);
};

export = nodeInit

```

im not sure what i am missing

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 March 2025 13:19 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/5 "2025-03-24T13:19:56Z")

</div>

> [@papenfort](#):
>
> `this.on("input", async (msg, send, done) => {`

↓ As I said ↓

> [@Steve-Mcl](#):
>
> Try using regular callbacks e.g. `function(x) { ... }` instead of `(x => ...)`

---

<div class="post-metadata">

**Author:** ![papenfort](https://avatars.discourse-cdn.com/v4/letter/p/73ab20/32.png) [@papenfort](https://discourse.nodered.org/u/papenfort)\
**Post date:** [24 March 2025 13:21 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/6 "2025-03-24T13:21:52Z")

</div>

i have already refactored it to use the function-keyword, to the same result

---

<div class="post-metadata">

**Author:** ![papenfort](https://avatars.discourse-cdn.com/v4/letter/p/73ab20/32.png) [@papenfort](https://discourse.nodered.org/u/papenfort)\
**Post date:** [24 March 2025 13:22 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/7 "2025-03-24T13:22:25Z")

</div>

the thing is, logging out the this keyword yields me something that looks like a valid node, just without \_flow

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 March 2025 13:25 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/8 "2025-03-24T13:25:49Z")

</div>

> [@papenfort](#):
>
> i have already refactored it to use the function-keyword

Including the `NodeInitializer`?

What does the final dist code look like. Is it in a repo we can look at?

---

<div class="post-metadata">

**Author:** ![papenfort](https://avatars.discourse-cdn.com/v4/letter/p/73ab20/32.png) [@papenfort](https://discourse.nodered.org/u/papenfort)\
**Post date:** [24 March 2025 13:39 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/9 "2025-03-24T13:39:54Z")

</div>

> [@papenfort](#):
>
> ```auto
> Red.nodes.createNode(
> this as Node<{}>,
> { ...config, outputs: 2 } as NodeDef
> );
> 
> ```

the problem lies in what i just quoted -.-  
turn out spreading the config doesnt copy **all** the fields, which went unnoticed by me.

---

<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:** [24 March 2025 18:45 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/10 "2025-03-24T18:45:32Z")

</div>

I've always found it extremely odd that the createNode function does not itself transfer the config properties to `this`.

The way you are doing things does not, I think, need you to create the `node` object. If you are only using arrow fns then `this` remains the same.

In fact, even if you create a standard function definition (at the module level) that you pass to the `registerType` callback, you still don't need to create a `node` object. You can continue to use `this` just fine. I very rarely have to create a `node` object any more - check out the link I shared.

---

<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:** [23 May 2025 18:46 UTC](https://discourse.nodered.org/t/node-send-causing-error-this-flow-is-undefined/96217/11 "2025-05-23T18:46:18Z")

</div>

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