I have a flow that compares two numerical variables, these values come from an HTTP request. Depending on the values of the two numeric variables, I SET another variable to a certain string :
IF VAR1 >VAR2 THEN MSG.TEST=VAR1 MORE IMPORTANT
IF VAR1 <VAR2 THEN MSG.TEST=VAR1 LESS IMPORTANT
IF VAR1 == VAR2 THEN MSG.TEST=OK
And now I JUST want to be able to retrieve the value of msg.TEST by making an HTTP request, so I searched on the internet how to do it, and I saw a flow that allows me to do what I want, and that is very simple, So I imported the flow :
So after that I just naively thought I'd make an HTTP request and then be able to see the beautiful values of my msg.result variable, but when I go to the web page to see the values I get :
I don't understand what I'm doing wrong, I don't know much about NodeRed I'm a real beginner so I don't even see a basic error that would be obvious to any regular user.
The function needs the two request and join to function.
So your http in would be the inject and the response after the function.
make sure msg.res is not overwritten it has to make it from the http in node to response node, set debugs to complete msg.
In node-red, each node / Function has no awareness of the last time it ran.
Therefore, "function 86" is ran fresh / clean every time a msg arrives.
What I mean by that is, when the HTTP Endpoint /test is triggered it fires a msg into "function 86- but it is NOT the same as themsg` your HTTP-Requests & the join node created. - a debug node before the function will clearly demonstrate this.
What to do
You need to store the value from the join node in context THEN retrieve if from context when the /test endpoint is called.
Another improvement you can make is to lose the join node & simplify things by performing the requests in series (store each result in context after the HTTP Requests)
In basic terms...
Flow 1.
inject -> HTTP Req -> Change Node (SET flow.reqOne to value of msg.payload) -> HTTP Req -> Change Node (SET flow.reqTwo to value of msg.payload)
Flow 2.
HTTP-in -> change node (set msg.payload.requestOne to value of flow.reqOne) -> change node (set msg.payload.requestTwo to value of flow.reqTwo) -> HTTP-Response
Thanks for ur reply,
I have a problem with this method, the two HTTP requests return the same variable, they just don't have the same values so if 2 different HTTP requests return a variable that has the same name
How do I differentiate this variable?
Like if the 1st HTTP request returns a variable named totalcount which is equal to 663
And if the 2nd HTTP request returns a variable named totalcount which is equal to 664
how do I differentiate between the totalcount from the 1st request and the totalcount from the 2nd request
You move them into unique named context variables as I said above (e.g. flow.reqOne and flow.reqTwo). You can then pick out what you need from the flow context - e.g using a change node with 2 entries, you can SET msg.payload.v1 to the appropriate flow variable e.g. flow.reqOne.totalcount and SET msg.payload.v2 to the appropriate flow variable e.g. flow.reqTwo.totalcount
Flow 1.
inject -> HTTP Req -> Change Node (SET flow.reqOne to value of msg.payload) -> HTTP Req -> Change Node (SET flow.reqTwo to value of msg.payload)
↑ See the 1st request has its msg.payload copied into flow.reqOne. The second request gets its msg.payload copied into flow.reqTwo.
Flow 2.
HTTP-in -> change node (set msg.payload.requestOne to value of flow.reqOne.totalcount) -> change node (set msg.payload.requestTwo to value of flow.reqTwo.totalcount) -> HTTP-Response
↑ Here, when the HTTP-In fires, you simply "grab" the values you wish from flow.reqOne and flow.reqTwo and build your payload (however you want) ready to be posted back in the HTTP-Response.