Passing 2 generated variables in msg.payload as strings

Hi all,

I am new here, so please excuse me if i'm posting this in the wrong topic.

I am using node-red to interface with my blockchain and need to pass the following arguments:

[2021-05-13T22:29:44.150] [DEBUG] BasicNetwork - channelName  : mychannel
[2021-05-13T22:29:44.151] [DEBUG] BasicNetwork - chaincodeName : Multicontracts_cc
[2021-05-13T22:29:44.151] [DEBUG] BasicNetwork - fcn  : CreateSensData
[2021-05-13T22:29:44.151] [DEBUG] BasicNetwork - args  : {"id":"4","name":"test2","temp":21,"time": 2021:05:11 20:00:00}

And i'm using the following code:

temp = msg.payload.d.ambientTemp;

var date = new Date();
var options = { timeZone: 'Europe/Malta', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' };
var time = date.toLocaleDateString('tr-TR', options);

msg.payload = {"fcn": "CreateSensData", 
"chaincodeName":"Multicontracts_cc",
"channelName": "mychannel", 
"args": ["{\"id\":\"4\",\"name\":\"test2\",\"addedAt\":1600065054,\"temp\": temp, \"time\": time"]};
msg.headers = {'content-type':'application/json'};

return msg;

Any ideas how can i pass the temp and time variables to the abive msg payload please?

Thanks!

Hi Krisbarb,

Why do you escape all the parameters in args?

You can create the object args first (the same way you do it with the msg.payload object) and assign it afterwards directly with „args“: args

Edit: Ah, or do you need the JSON-String inside? Check this: JSON.stringify() - JavaScript | MDN

Cheers
Ranki

Thanks for your reply :slight_smile:

Yes JSON-string inside

I edited my entry; have a look here: JSON.stringify() - JavaScript | MDN

1 Like

Appreciate it mate! Let me have a look at this function

@Ranki do you have experience with this function, can i ask you how would you use it with my above code?

Thanks once again for your help :slight_smile:

You can add time var string to the msg.payload string like below

msg.payload = {"fcn": "CreateSensData", 
"chaincodeName":"Multicontracts_cc",
"channelName": "mychannel", 
"args": ["{\"id\":\"4\",\"name\":\"test2\",\"addedAt\":1600065054,\"temp\": temp, \"time\": \"" + time +"\""]};

you can also add temp

"args": ["{\"id\":\"4\",\"name\":\"test2\",\"addedAt\":1600065054,\"temp\": \"" + temp + "\", \"time\": \"" + time +"\""]};```
1 Like

Thanks, will give it a try tonight. much appreciate it :slight_smile:

also you can do this using single quotes around string.

 msg.payload = {"fcn": "CreateSensData", 
"chaincodeName":"Multicontracts_cc",
"channelName": "mychannel", 
"args": ['{"id":"4","name":"test2","addedAt":1600065054,"temp": "' + temp + '", "time": "' + time + '"']};

or this using backticks around string and use ${var}

 msg.payload = {"fcn": "CreateSensData", 
"chaincodeName":"Multicontracts_cc",
"channelName": "mychannel", 
"args": [`{"id":"4","name":"test2","addedAt":1600065054,"temp": "${temp}", "time": "${time}"`]};
1 Like

Hi @krisbarb,

I implemented your code and adjusted it with the stringify-function:

var temp = 23;
var date = new Date();
var options = { timeZone: 'Europe/Malta', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' };
var time = date.toLocaleDateString('tr-TR', options);

var args = {"id":"4","name":"test2","addedAt":1600065054,"temp": temp, "time": time};
msg.payload = {"fcn": "CreateSensData", 
"chaincodeName":"Multicontracts_cc",
"channelName": "mychannel", 
"args": JSON.stringify(args)};
msg.headers = {'content-type':'application/json'};

return msg;

The result looks like this; I think this is, what you wanted to achive, isn't it?

Cheers
Ranki

1 Like

Perfect. Thanks a mill!

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