var host0 = msg.payload.response[0].hostname;
var host0IP = msg.payload.response[0].managementIpAddress;
var host0S = msg.payload.response[0].reachabilityStatus;
var host1 = msg.payload.response[1].hostname;
var host1IP = msg.payload.response[1].managementIpAddress;
var host1S = msg.payload.response[1].reachabilityStatus;
msg.payload= {"body":{"roomId": "Y2lzY29zcGFyazovL3VzL1JPT00vMmEzYzRjMDAtNTliOC0xMWU5LWEzZjktNGRjNmI1MmU3MTE3", "text": "Network Device Details: \n
Device 1: Host Name- "+host0+",Management IP- "+host0IP+", Reachability Status- "+host0S+" \n
Device 2: Host Name- "+host1+" , Management IP- "+host1IP+" , Reachability Status- "+host1S+" "}};
return msg;
Ended up with Unclosed string error!
ApiAsylum:
\n
Take a look at the end of each line with a next to it. What do you see?
side issue where you have Host Name- "+host0+"
, if host0 was 123
the result wll be Host Name- 123
secondly watch the +
at the end of the lats variable - do you really want it?
I gotta declare a total of 13 devices as a list hence using \n at the end.
generally I find that:
msg.payload = msg.payload + " the next bit"
is more readable
Can you explain this msg.payload = msg.payload + " the next bit"
? i'm not getting it
you can't use \n
in the function to extend the lines. put it all on one line and see what happens.
But I agree with @ukmoose I would use what he showed or
msg.payload += " the next bit';
it is suppose get printed in this format!
Then you need to ensure the \n is within the string and not interpreted as an end of line
msg.payload= {"body":{"roomId": "Y2lzY29zcGFyazovL3VzL1JPT00vMmEzYzRjMDAtNTliOC0xMWU5LWEzZjktNGRjNmI1MmU3MTE3"};
msg.payload.body.text = "Network Device Details: \n"
msg.payload.body.text = msg.payload.body.text + "Device 1: Host Name-"+host0+",Management IP- "+host0IP+", Reachability Status- "+host0S+"\n";
msg.payload.body.text = msg.payload.body.text + " Device 2: Host Name- "+host1+" , Management IP- "+host1IP+" , Reachability Status- "+host1S+" "}};
1 Like
thank you, it's working now after scripting in this format
sg.payload= {"body":{"roomId": "Y2lzY29zcGFyazovL3VzL1JPT00vMmEzYzRjMDAtNTliOC0xMWU5LWEzZjktNGRjNmI1MmU3MTE3", "text": "Network Device Details:\n Device 1: Host Name- "+host0+",Management IP- "+host0IP+", Reachability Status- "+host0S+"\n Device 2: Host Name- "+host1+" , Management IP- "+host1IP+" , Reachability Status- "+host1S+"\n Device 3: Host Name- "+host2+" , Management IP- "+host2IP+" , Reachability Status- "+host2S+"\n Device 4: Host Name- "+host4+" , Management IP- "+host4IP+" , Reachability Status- "+host4S+"\nDevice 5: Host Name- "+host5+" , Management IP- "+host5IP+" , Reachability Status- "+host5S+"\nDevice 6: Host Name- "+host6+" , Management IP- "+host6IP+" , Reachability Status- "+host6S+"\nDevice 7: Host Name- "+host7+" , Management IP- "+host7IP+" , Reachability Status- "+host7S+"\n Device 8: Host Name- "+host8+" , Management IP- "+host8IP+" , Reachability Status- "+host8S+"\nDevice 9: Host Name- "+host9+" , Management IP- "+host9IP+" , Reachability Status- "+host9S+"\nDevice 10: Host Name- "+host10+" , Management IP- "+host10IP+" , Reachability Status- "+host10S+"\nDevice 11: Host Name- "+host11+" , Management IP- "+host11IP+" , Reachability Status- "+host11S+"\nDevice 12: Host Name- "+host12+" , Management IP- "+host12IP+" , Reachability Status- "+host12S+"\nDevice 13: Host Name- "+host13+" , Management IP- "+host13IP+" , Reachability Status- "+host13S+""}};
Don't forget that, assuming you are using Node.js v8.5+ and a recent browser, you can use JavaScript template strings (using backticks instead of quotes). Template strings allow you to have embedded line feeds without having to us \n
.
const myVar = 42
const myString = `This is some text ...
with an embedded new line.
Also, template strings allow embedded JavaScript: ${myVar}`
2 Likes
@TotallyInformation I learn something new every day. Thanks Julian!
1 Like