# How to use msg.payload of a function into javascript?

**URL:** <https://discourse.nodered.org/t/how-to-use-msg-payload-of-a-function-into-javascript/66626>\
**Category:** General\
**Created:** [18 August 2022 22:06 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-of-a-function-into-javascript/66626 "2022-08-18T22:06:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Macko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/macko/32/66450_2.png) [@Macko](https://discourse.nodered.org/u/Macko)\
**Post date:** [18 August 2022 22:06 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-of-a-function-into-javascript/66626/1 "2022-08-18T22:06:30Z")

</div>

I have a function that pulls information from a database, I've even saved it to a global variable.  
The problem is that I don't know how to use this value in javascript, I have tried with global.get() but the console gives me an error because global is not declared as a variable.

I am attaching the script, the global variable is saved in "resultado", and I want to use it as "mensaje" inside js.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/2806efc5ce28460e8efc23a2132680eb2b0bb640.png)

---

<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:** [18 August 2022 22:24 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-of-a-function-into-javascript/66626/2 "2022-08-18T22:24:06Z")

</div>

The template node is for templating text (primarily) for the browser. It uses mustache syntax (as the built in help states) to access `msg` and `context` values...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f69862061dfcd53c4a775095ef9309279165eb9f.png)

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [18 August 2022 22:39 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-of-a-function-into-javascript/66626/3 "2022-08-18T22:39:38Z")

</div>

Use function node, if you want to code in JS.

---

<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:** [18 August 2022 22:49 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-of-a-function-into-javascript/66626/4 "2022-08-18T22:49:47Z")

</div>

The OP is writing client side code (note the use of `document.getElementById`)

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [18 August 2022 22:52 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-of-a-function-into-javascript/66626/5 "2022-08-18T22:52:50Z")

</div>

But the first line is trying to get a value from the global context...

So this is going to need some pretty major redesign to get things to work...

---

<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:** [18 August 2022 22:55 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-of-a-function-into-javascript/66626/6 "2022-08-18T22:55:14Z")

</div>

Pretty sure `{{global.myvar}}` works in a template

---

<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:** [17 October 2022 22:56 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-of-a-function-into-javascript/66626/7 "2022-10-17T22:56:02Z")

</div>

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