Search code examples
javascriptweb-servicesnode-red

Extract from a NODE-RED string


I have my NODE-RED schemma:

enter image description here

following string result from my "Function" node my node:

msg.payload : string[63]
"{"random":{"date":"22:55","random":21},"time":{"time":"22:52"}}"

This is the code of my "Function Node":

msg.payload.random=context.global.randomandtime;
msg.payload.time=context.global.time;

return msg;

I need to put in "part of the string" (not all) like this =>{"date":"22:55","random":21} and show it in my browser like a webpage but not using html tags.

Like this:

22:55

21

Any help will be wellcome.


I have added template(Mustache) and I am traying to bring data to it,(Note:http response is already in schemme but not shown here)

enter image description here

I am traying to bring data here (template). But I get error.

The Mustache template body is:

This is the payload: {{#payload.randomandandtime.random}} !

But I have back this error back:

2017-5-18 16:18:00node: Mustachemsg : string[56]

"Unclosed section "payload.randomandandtime.random" at 59"

In browser I get

502 Bad Gateway: Registered endpoint failed to handle the request.

Even If I change it only payload.randomandandtime I get empty:

payload.randomandandtime

In browser & console:

Messsage received back: (empty) This is the payload: !


Solution

  • Finally I solved in this way. I make all in one Global varaible instead 2 global variables. I passed it to mustache template and in Mustache I worked with context in order to get it.

    General Scheme: enter image description here

    Then in recoverydata:

    msg.payload = context.global.get("randomtime");
    

    In My Mustache:

    `{{#payload.random}}
    Last random number request returned {{&payload.random}}, which was received 
    at {{&payload.randomtime.times}}{{/payload.random}}
    {{/payload}}`
    

    The resul of it is a Webservice not using HTML and this is:

    url https://....../page

    "Time last server time request received at 13:14 Last random number request returned 94, which was received at 13:14"