Save value from a request

Hi,

I want to save msg.keyID from a request, which returns more than 1 times. I think a picture maybe good to demonstrate.
One time request return these value..

and here is my currently code

[{"id":"e2a6bf49.da7258","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"ad06247.c845bd8","type":"ui_button","z":"e2a6bf49.da7258","name":"GET Keys","group":"b8575647.ebefb","order":6,"width":0,"height":0,"passthru":false,"label":"GET DEFINED OBJECTS","tooltip":"","color":"","bgcolor":"","icon":"","payload":"1","payloadType":"num","topic":"","x":70,"y":160,"wires":[["fc127236.89ff4"]]},{"id":"fc127236.89ff4","type":"function","z":"e2a6bf49.da7258","name":"GET Keys","func":"return {payload : \"KEYS *\"};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":230,"y":160,"wires":[["b260546d.7c5e1"]]},{"id":"b260546d.7c5e1","type":"http request","z":"e2a6bf49.da7258","name":"Get Keys","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://localhost:9851/{{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":380,"y":160,"wires":[["eccbf81f.7a8508"]]},{"id":"eccbf81f.7a8508","type":"function","z":"e2a6bf49.da7258","name":"Response","func":"var definedKeys = msg.payload.keys;\nreturn {payload : definedKeys};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":160,"wires":[["31658d6b.039eca"]]},{"id":"ee088fb8.f544b","type":"http request","z":"e2a6bf49.da7258","name":"HTTP GET REQUEST","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://localhost:9851/{{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":460,"y":300,"wires":[["3741ec9.6c07394"]]},{"id":"3741ec9.6c07394","type":"debug","z":"e2a6bf49.da7258","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":240,"wires":[]},{"id":"31658d6b.039eca","type":"split","z":"e2a6bf49.da7258","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":110,"y":300,"wires":[["25cfb7bd.8a95b8"]]},{"id":"25cfb7bd.8a95b8","type":"function","z":"e2a6bf49.da7258","name":"","func":"msg.keyID = msg.payload;\nmsg.payload = 'SCAN%20'+msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":280,"y":300,"wires":[["ee088fb8.f544b"]]},{"id":"dac2dc01.50f688","type":"ui_text","z":"e2a6bf49.da7258","group":"b8575647.ebefb","order":1,"width":0,"height":0,"name":"","label":"KeyID","format":"{{msg.keyID}}","layout":"row-spread","x":640,"y":740,"wires":[]},{"id":"18cb72ac.e7224d","type":"ui_text","z":"e2a6bf49.da7258","group":"b8575647.ebefb","order":2,"width":0,"height":0,"name":"","label":"Position","format":"{{msg.payload}}","layout":"row-spread","x":650,"y":820,"wires":[]},{"id":"bc020133.4241d","type":"json","z":"e2a6bf49.da7258","name":"","property":"payload","action":"","pretty":false,"x":310,"y":680,"wires":[[]]},{"id":"b8575647.ebefb","type":"ui_group","z":"","name":"Default","tab":"d9869613.8ce11","order":1,"disp":true,"width":"6","collapse":false},{"id":"d9869613.8ce11","type":"ui_tab","z":"","name":"Case 10: Get all defined objects","icon":"dashboard","order":10,"disabled":false,"hidden":false}]

Thanks in advance.

Where is it that you want to use the id?

1 Like

Situation is:

  • I am using platform "Tile38", which uses to define and determine geology subject.
  • I want to get all defined objects in Tile38. Every object has a keyID, a filed name, longitutde and latitude (position of object). It could be, more than one object has the same keyID but different filed name.

My plan is:

  • Firstly, get all defined keyID in database with a URL request "KEY *".
  • Secondly, using these keyID to get field name and position of objects with another URL request "SCAN*".

I have wrote a small Java program with this strategy and it works fine. I have no experience with Javascript but I think it should work same.

Sorry for my bad English.

Let me ask the question another way, what doesn't work in the flow you posted?

1 Like

It works but like you see it returns 3 times for 3 keyID in just one time request. It's maybe bigger if I define more object in platform. I want to save 3 keyID, field name, position in a map or dictionary, whatever data structure which have key, value with key = keyID + field name, value = position object and then display these objects in a text output.

Sorry, I don't understand what you mean, do you mean that bit is working correctly but now you want to save those so you can use them later? If that is what you mean do you actually need to save them or do you just want to display them?

If you want to save the data then have a look at the node red docs page Working with Context which will show you how you can save the data in an object and then use it later.

1 Like

Right, I want to save them to use later and also display them. And the text element in nodered dashboard doesn't work for this situation, do you have any suggestion?

What do you want the display to look like? Sketch what you would like to see from the debug that you posted earlier.

1 Like

@torrent

It is not 100% clear what you want so I did a little bit of guess work and generated a flow that does this...

  1. add an object to flow context called results
  2. upon each HTTP GET REQ, add the payload into the flow.results object under the key 'keyID`
[{"id":"5518f8ee.37a1f8","type":"http request","z":"f057b3e5.0b042","name":"Get Keys","method":"GET","ret":"obj","paytoqs":true,"url":"http://localhost:9851/{{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":520,"y":80,"wires":[["f5dfb751.43a2c8"]]},{"id":"2fe3f84b.4cd0b8","type":"http request","z":"f057b3e5.0b042","name":"HTTP GET REQUEST","method":"GET","ret":"obj","paytoqs":true,"url":"http://localhost:9851/{{payload}}","tls":"","persist":false,"proxy":"","authType":"","x":580,"y":160,"wires":[["50e1504e.9179a"]]},{"id":"15ede35b.a9535d","type":"split","z":"f057b3e5.0b042","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":250,"y":160,"wires":[["ecc212dd.0fec1"]]},{"id":"ecc212dd.0fec1","type":"function","z":"f057b3e5.0b042","name":"","func":"msg.keyID = msg.payload;\nmsg.query = 'SCAN%20' + msg.payload;\nmsg.payload = msg.query;\nreturn msg;","outputs":1,"noerr":0,"x":400,"y":160,"wires":[["2fe3f84b.4cd0b8"]]},{"id":"cf93726d.aa6b1","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":80,"wires":[["1c96af81.c5fe2"]]},{"id":"1c96af81.c5fe2","type":"change","z":"f057b3e5.0b042","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"KEYS *","tot":"str"},{"t":"set","p":"results","pt":"flow","to":"{}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":80,"wires":[["5518f8ee.37a1f8"]]},{"id":"f5dfb751.43a2c8","type":"change","z":"f057b3e5.0b042","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.keys","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":80,"wires":[["15ede35b.a9535d"]]},{"id":"50e1504e.9179a","type":"function","z":"f057b3e5.0b042","name":"Store in flow.results","func":"//store payload from HTTP GET REQ\n//in the results object, using keyID as the key\n\nvar results = flow.get(\"results\") || {};//get the results object\nresults[msg.keyID] = msg.payload; //add this result\nflow.set(\"results\", results); //update flow storage\n\nreturn msg;","outputs":1,"noerr":0,"x":820,"y":160,"wires":[[]]}]

Give it a go, then check the context tab on the side-bar - see if it works for you.

NOTE: I cannot test this as I dont have access to your URL localhost:9851

1 Like

The output should look like:
"Object with keyID "+ keyID + " and field name "+ fieldName +" longitude: "+longitude+" latitude "+latitude
and it displays with this form for all objects

Hi, you could try this platform from Docker.

docker pull tile38/tile38
docker run -p 9851:9851 tile38/tile38

Not going there :stuck_out_tongue:


Does my solution work for you?
Do you understand what the intention is?
Have you looked in the context viewer to see if there are any values?
Do you know what flow context is?

Perhaps if you expand the payload object returned from HTTP GET REQ in the debug window and sent a screen shot and copy the output (using the copy button on the debug window) then paste it ...
```
like this
```
... i might have a chance of understanding what is returned

It doesn't work but I got your idea. Thank you so much. You save my weekend! :heart_eyes:
The only thing now is display my object, which we don't know exactly how many objects :face_with_monocle:

I haven't used it but perhaps the ui-table node might be useful.

1 Like

Thank you, guys. It truely works for me

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