How set the output variable "{{msg.payload}}" in JS?!

Ok, here is an example flow with a function that picks a random company name (out of a list) and a random number for the Stock price. It puts them in a flow variable and limits the number to 10 entries. Updates happen every 3 seconds.

It then sends the array to the template node and you can watch the new entry appear at the top of the list and the 10 entry gets pushed off the bottom. this should give you an idea of what you have to do.

[{"id":"9ea1913.9974bf","type":"inject","z":"968ca76f.3dc46","name":"Go","topic":"","payload":"1","payloadType":"str","repeat":"3","crontab":"","once":true,"onceDelay":"","x":90,"y":280,"wires":[["f56e7bfc.ca7fa"]]},{"id":"f56e7bfc.ca7fa","type":"function","z":"968ca76f.3dc46","name":"build array (2) random","func":"var x = [];\nvar companyStock = flow.get(\"companyData\") || x;\nvar arrayCompany = [\"Apple\",\"AT&T\",\"Berkshire Hathaway\",\"BP\",\"China National Petroleum\",\"CVS Health\",\"Daimler\",\"Exor\",\"Exxon Mobil\",\"General Motors\",\"Glencore\",\"McKesson\",\"Royal Dutch Shell\",\"Samsung Electronics\",\"Sinopec Group\",\"State Grid\",\"Toyota\",\"United Health\",\"Volkswagen Group\",\"Walmart\"];\nvar ixCompany = Math.floor(Math.random() * (20));\nvar stockPrice = Math.floor(Math.random() * (1000))/10;\nvar latest = [arrayCompany[ixCompany], stockPrice];\ncompanyStock.unshift(latest);\nif (companyStock.length > 10) {companyStock.pop();}\nflow.set(\"companyData\",companyStock);\nmsg.payload = companyStock;\nreturn msg;","outputs":1,"noerr":0,"x":280,"y":280,"wires":[["6c5af6fb.7c03d8"]]},{"id":"62fa919c.32ffc","type":"debug","z":"968ca76f.3dc46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":770,"y":280,"wires":[]},{"id":"6c5af6fb.7c03d8","type":"ui_template","z":"968ca76f.3dc46","group":"afe1abd8.451f38","name":"Two dimension array table","order":1,"width":"12","height":"10","format":"\n<table id=\"table\" border=\"1\">\n <tr>\n <th>Company</th> \n <th>Latest Price</th>\n </tr>\n <tbody>\n <tr ng-repeat=\"row in msg.payload\">\n <td ng-repeat=\"item in row\" >{{item}}</td>\n </tr>\n </tbody>\n</table>\n\n","storeOutMessages":false,"fwdInMessages":true,"templateScope":"local","x":540,"y":280,"wires":[["62fa919c.32ffc"]]},{"id":"afe1abd8.451f38","type":"ui_group","z":"","name":"Column 2","tab":"5b3bb364.7ec764","order":2,"disp":false,"width":"12","collapse":false},{"id":"5b3bb364.7ec764","type":"ui_tab","z":"","name":"Feeder test","icon":"dashboard"}]

1 Like