# From text to variable

**URL:** <https://discourse.nodered.org/t/from-text-to-variable/34850>\
**Category:** General\
**Created:** [26 October 2020 18:30 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850 "2020-10-26T18:30:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)\
**Post date:** [26 October 2020 18:30 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/1 "2020-10-26T18:30:08Z")

</div>

I have some info in a database one of the fiels is `msg.code[i]`  
When i get this back from the database it is between quotes `"msg.code[i]"`  
How can i convert the value too a variable?  
Just removing the "" wasnt working

Thanks  
Rogier

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 October 2020 19:12 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/2 "2020-10-26T19:12:21Z")

</div>

Why have you stored `"msg.code[i]"` in the database? Did you actually want to store its value originally?

You cant get back the value it had at that point in time because you haven't stored it.

---

<div class="post-metadata">

**Author:** ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)\
**Post date:** [26 October 2020 19:36 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/3 "2020-10-26T19:36:01Z")

</div>

i always try to make my question simple  
but something to much i guess...

in my database i have different email templates  
for every template there a different replacement actions

for example  
[["||code||","msg.users[i].code]"],["||pin||","msg.users[i].pin"]]

||code|| needs to be replaced with msg.users[i].code  
and  
||pin|| needs to be replaced with msg.users[i].pin  
and so on

i made a loop the checks the count and then does every action

the value msg.users[i].pin has been get a loop before from the sql database

hope you understand this in this way

Rogier

---

<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:** [26 October 2020 20:14 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/4 "2020-10-26T20:14:12Z")

</div>

If I am reading this right, you have stored `msg.users[i].pin` in the database and you want to get the value (by path) from a `msg` object?

If so, you could use [eval](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)

```auto
var msg = { users : [{"code":"abc","pin":123},{"code":"xyz","pin":789}] }
var path = "msg.users[i].pin"
var i = 0;
var result = eval(path);

```

demo flow...

```auto
[{"id":"e6eba960.aa1648","type":"inject","z":"872f5cc1.67e9c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":620,"wires":[["4f1ee4f5.2951dc"]]},{"id":"a1cf8d91.dd45b","type":"function","z":"872f5cc1.67e9c","name":"","func":"\nvar path = \"msg.users[i].pin\"\nvar i = 0;\nmsg.payload = eval(path);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":590,"y":620,"wires":[["25315b54.f5ae04"]]},{"id":"25315b54.f5ae04","type":"debug","z":"872f5cc1.67e9c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":620,"wires":[]},{"id":"4f1ee4f5.2951dc","type":"change","z":"872f5cc1.67e9c","name":"","rules":[{"t":"set","p":"users","pt":"msg","to":"[{\"code\":\"abc\",\"pin\":123},{\"code\":\"xyz\",\"pin\":789}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":620,"wires":[["a1cf8d91.dd45b"]]}]

```

---

<div class="post-metadata">

**Author:** ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)\
**Post date:** [26 October 2020 20:41 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/5 "2020-10-26T20:41:07Z")

</div>

Thanks for creating an example for me!  
Unfortunately is not exactly what i need.

find it hard to explain in other way  
what it is

```auto
var xx = "msg.users[i].pin";

```

what it needs to be

```auto
var yy = msg.users[i].pin;

```

eval() is not working, i tried

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 October 2020 20:44 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/6 "2020-10-26T20:44:17Z")

</div>

`eval()` is what you want... it takes a string and evaluates it as if it was code. So assuming `msg` and `i` are both defined when you call eval, then it should give you the result you want.

If it isn't working, then something is missing in what you've shared. You'll have to debug into figure out what piece is missing or incorrect.

---

<div class="post-metadata">

**Author:** ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)\
**Post date:** [26 October 2020 20:45 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/7 "2020-10-26T20:45:11Z")

</div>

ok thanks i go on trying!! 😉

---

<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:** [26 October 2020 20:54 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/8 "2020-10-26T20:54:12Z")

</div>

> [@RogierQ](#):
>
> ```auto
> var xx = "msg.users[i].pin";
> 
> ```
> 
> what it needs to be
> 
> ```auto
> var yy = msg.users[i].pin;
> 
> ```
> 
> eval() is not working, i tried

That is exactly what I did in my demo....

> [@Steve-Mcl](#):
>
> ```auto
> var path = "msg.users[i].pin"
> var i = 0;
> var result = eval(path);
> 
> ```

however the above code snippet was for you to see how `eval` is used - but to all intents and purposes, it is the same as the below...  
`var xx = eval( "msg.users[i].pin" );`

* * *

Did you import the demo flow where i did it with a real msg and function node?

_Obviously in the demo, I hard coded the `path` (as I dont have your database) or messages to play with_

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 October 2020 21:09 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/9 "2020-10-26T21:09:42Z")

</div>

Add warn statements before the eval line containing something like

```auto
node.warn(`i: ${i}`)
node.warn(msg.users[i].code)

```

and check that the data looks good.

---

<div class="post-metadata">

**Author:** ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)\
**Post date:** [26 October 2020 21:13 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/10 "2020-10-26T21:13:08Z")

</div>

yes i tried the example, thanks for taking time to create it!

i think i got it working now with eval()  
only "problem" is that i dont now what i did wrong before

thanks for your support!

---

<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 2020 21:13 UTC](https://discourse.nodered.org/t/from-text-to-variable/34850/11 "2020-12-25T21:13:11Z")

</div>

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