From text to variable

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

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.

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

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

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

[{"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"]]}]

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

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

what it needs to be

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

eval() is not working, i tried

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.

ok thanks i go on trying!! :wink:

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

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

Add warn statements before the eval line containing something like

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

and check that the data looks good.

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!

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