# Unclosed string

**URL:** <https://discourse.nodered.org/t/unclosed-string/10458>\
**Category:** General\
**Created:** [23 April 2019 11:03 UTC](https://discourse.nodered.org/t/unclosed-string/10458 "2019-04-23T11:03:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ApiAsylum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/apiasylum/32/7486_2.png) [@ApiAsylum](https://discourse.nodered.org/u/ApiAsylum)\
**Post date:** [23 April 2019 11:03 UTC](https://discourse.nodered.org/t/unclosed-string/10458/1 "2019-04-23T11:03:33Z")

</div>

```auto
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!

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [23 April 2019 11:10 UTC](https://discourse.nodered.org/t/unclosed-string/10458/2 "2019-04-23T11:10:28Z")

</div>

> [@ApiAsylum](#):
>
> \n

Take a look at the end of each line with a ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/281944811f60a1ccc4977c7890efc4a93065c2c4.png) next to it. What do you see?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 April 2019 11:11 UTC](https://discourse.nodered.org/t/unclosed-string/10458/3 "2019-04-23T11:11:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ApiAsylum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/apiasylum/32/7486_2.png) [@ApiAsylum](https://discourse.nodered.org/u/ApiAsylum)\
**Post date:** [23 April 2019 11:15 UTC](https://discourse.nodered.org/t/unclosed-string/10458/4 "2019-04-23T11:15:45Z")

</div>

I gotta declare a total of 13 devices as a list hence using _\n_ at the end.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [23 April 2019 11:19 UTC](https://discourse.nodered.org/t/unclosed-string/10458/5 "2019-04-23T11:19:09Z")

</div>

generally I find that:

`msg.payload = msg.payload + " the next bit"`

is more readable

---

<div class="post-metadata">

**Author:** ![ApiAsylum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/apiasylum/32/7486_2.png) [@ApiAsylum](https://discourse.nodered.org/u/ApiAsylum)\
**Post date:** [23 April 2019 11:26 UTC](https://discourse.nodered.org/t/unclosed-string/10458/6 "2019-04-23T11:26:28Z")

</div>

Can you explain this `msg.payload = msg.payload + " the next bit"`? i'm not getting it

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 April 2019 11:27 UTC](https://discourse.nodered.org/t/unclosed-string/10458/7 "2019-04-23T11:27:17Z")

</div>

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';`

---

<div class="post-metadata">

**Author:** ![ApiAsylum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/apiasylum/32/7486_2.png) [@ApiAsylum](https://discourse.nodered.org/u/ApiAsylum)\
**Post date:** [23 April 2019 11:30 UTC](https://discourse.nodered.org/t/unclosed-string/10458/9 "2019-04-23T11:30:43Z")

</div>

it is suppose get printed in this format!

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a9f2088f0490ee83c925d68e9f6543c8a217681c.png)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [23 April 2019 11:34 UTC](https://discourse.nodered.org/t/unclosed-string/10458/10 "2019-04-23T11:34:37Z")

</div>

Then you need to ensure the \n is within the string and not interpreted as an end of line

```auto
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+" "}};

```

---

<div class="post-metadata">

**Author:** ![ApiAsylum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/apiasylum/32/7486_2.png) [@ApiAsylum](https://discourse.nodered.org/u/ApiAsylum)\
**Post date:** [23 April 2019 11:46 UTC](https://discourse.nodered.org/t/unclosed-string/10458/11 "2019-04-23T11:46:00Z")

</div>

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+""}};`

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 April 2019 12:15 UTC](https://discourse.nodered.org/t/unclosed-string/10458/12 "2019-04-23T12:15:10Z")

</div>

> [@zenofmud](#):
>
> you can't use `\n` in the function to extend the lines. put it all on one line and see what happens.

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`.

```javascript
const myVar = 42

const myString = `This is some text ...
with an embedded new line.
Also, template strings allow embedded JavaScript: ${myVar}`
```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 April 2019 13:15 UTC](https://discourse.nodered.org/t/unclosed-string/10458/13 "2019-04-23T13:15:58Z")

</div>

@TotallyInformation I learn something new every day. Thanks Julian!
