Define a new message

How define a new topic in a function and use the result of this topic.

Have you looked here ... Creating your first flow : Node-RED

Define a message or topic or msg.topic?

What are you asking and what are you trying to achieve?

function...

let topic = "hello, I am a new variable named topic"

const m = {} // a new message object
m.topic = topic // create a new var in the object & setting it to value of var topic

const m2 = {
  topic: "a new object with a topic property defined in one go"
} // a new message object

const m3 = {
  topic: topic //setting topic in a new object using a var
  payload: msg.payload + " - with some extra text on the end"
} // a new message object

1 Like

I have two query in mysql. I put the first query in the input message and i have created a new topic in a function with the second query and i need to recuperate the result of this query.

As Steve said:

const m = {
	payload = <first_query>
	topic = <second_query>
}

return m

It might help if you could publish the NR flow you have created so far as I feel a number of people are trying to imagine what you are trying to do.

In the input, i have put the first sql query and in the function i have this code :

msg.topic = "call GetInfo_nad(:id_ref,val1, val2,val3);SELECT val1, val2,val3;";
return msg;

Change the debug node and set it to eithor display msg.topic or set it to display the 'complete msg object'. That way you will see everything in the msg.

Yes i have changed it but i have to use a new topic to take the result of the storage procedure

@salma2302 it is very unclear what you are asking.

Could you please spell out what you are trying to achieve in step by step terms please?

Q: Have you installed the node-red-node-mysql node in the palette?

You need to feed a SQL Query to a mysql node to get results. Then, you can feed that to another node (like a function) to build a 2nd SQL Query --> then send that to a mysql node then use them results.

In a table in mysql I have id that I need to retrieve and put in a stored procedure as a parameter. So i need to use the first query sql in the input of msg (in msg.topic) after that i use the function to retrieve stored procedure output parameters
.

Pictures tell us almost nothing. Please try to post the flow, here are instructions on how to do that: Importing and Exporting Flows : Node-RED

Also wrap the flow in triple ``` backticks ``` when you post it, so it will be formatted correctly.

I do not understand what you are trying to achieve, but it sounds as if you should be able to do it all in the definition of your mysql stored procedure.

Perhaps you could share the code of your procedure and examples of the data you pass into it and what you want to get out?

Here is how I would (if I understand what you are doing) set up the flow:

[{"id":"fec4b3d3a6877eec","type":"inject","z":"ac7c483605966282","name":"Query to get ID","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":180,"wires":[["84963f5074f64fbd"]]},{"id":"4c7a1bf542651113","type":"mysql","z":"ac7c483605966282","name":"","x":730,"y":100,"wires":[[]]},{"id":"36a4ec38f06dd716","type":"function","z":"ac7c483605966282","name":"build query to get info","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":180,"wires":[["a00fba52c0052cfd"]]},{"id":"84963f5074f64fbd","type":"link call","z":"ac7c483605966282","name":"","links":["91583244a2f9421c"],"linkType":"static","timeout":"30","x":450,"y":180,"wires":[["36a4ec38f06dd716","6ec5e13077894b29"]]},{"id":"91583244a2f9421c","type":"link in","z":"ac7c483605966282","name":"MySQL link","links":[],"x":550,"y":100,"wires":[["2a47b3d5695b8fc5"]],"l":true},{"id":"ced2b963ad95a6e6","type":"link out","z":"ac7c483605966282","name":"return","mode":"return","links":[],"x":930,"y":100,"wires":[],"l":true},{"id":"a00fba52c0052cfd","type":"link call","z":"ac7c483605966282","name":"","links":["91583244a2f9421c"],"linkType":"static","timeout":"30","x":850,"y":180,"wires":[["96f6e5f137ce1d35"]]},{"id":"2a47b3d5695b8fc5","type":"change","z":"ac7c483605966282","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"in the link","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":40,"wires":[["ced2b963ad95a6e6"]]},{"id":"6ec5e13077894b29","type":"debug","z":"ac7c483605966282","name":"debug 19","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":280,"wires":[]},{"id":"96f6e5f137ce1d35","type":"debug","z":"ac7c483605966282","name":"debug 20","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1000,"y":280,"wires":[]}]

You build the query to get the ID and send that to the link node. It sends the msg to the mysql node (you need to change the wires to connect to the mysql node, the change node is there just to show how it works) then the output will be sent back to the link node and passed on to the function node where you can build your second sql statement. Then you use a second link node to send the request to the mysql node and the result is returned back to the second link node and you can do what you want with it.

Yes, that's what i wanted to do but in the function build topic i have created a msg.topic = "call storage_procedure(id, param1,param2); select param1, param2" and i need to use this new topic to display the results of the storage procedure

Exactly what do you mean - where do you want to display the results?

I think you can see that people are having trouble understanding what you are attempting to do. Please help us by sharing your code.

This is the msg topic and i need to use the topic to creat a new message payload to use the output of the function

Clearly you have to pass the message above to the mysql node.
The output of your query
Untitled 1
will be in the msg.payload returned from the mysql node.

So you need a flow like this

If this is not the solution you are looking for, supply the information people have asked for.

Thank you so much, this is the result i was looking for.