# Help - what am I doing wrong

**URL:** https://discourse.nodered.org/t/help-what-am-i-doing-wrong/99251
**Category:** General
**Tags:** function-node
**Created:** [3 October 2025 11:40 UTC](https://discourse.nodered.org/t/help-what-am-i-doing-wrong/99251 "2025-10-03T11:40:55Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Linhey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/linhey/32/73679_2.png) [@Linhey](https://discourse.nodered.org/u/Linhey)
#### Post date: [3 October 2025 11:40 UTC](https://discourse.nodered.org/t/help-what-am-i-doing-wrong/99251/1 "2025-10-03T11:40:55Z")

</div>

I have a Satellite 2 line Kepler list, which is in a strict format and includes white spaces, which cannot be removed as they form the correct sequence for reading.  
However the satellite name can have the white spaces removed.

I have my list for each sat like this

"SAUDISAT 1C (SO-50)  
1 27607U 02058C 25275.89343148 .00004387 00000+0 54779-3 0 9994  
2 27607 64.5534 204.4946 0067520 310.0132 49.5059 14.82017782226316"

Which I have now formatted to this

array[3]  
0: object  
col1: "SAUDISAT 1C (SO-50) _whitespaces here to remove_ "  
1: object  
col1: "1 27607U 02058C 25275.89343148 .00004387 00000+0 54779-3 0 9994"  
2: object  
col1: "2 27607 64.5534 204.4946 0067520 310.0132 49.5059 14.82017782226316"

_Note: This editor removes the multiple white spaces everywhere_

I want to remove the spaces from the end of the Satellite name as it is easier to select in a switch node so I did this, which looks reasonable to me, but the output I get is triplicated, even though it does what I want.

```auto
function trim(sat) {
    let trimsat = sat.trim();
    msg.payload.sat = trimsat;
}

for (var i = 0; i < (msg.payload).length; i++) {   
    
    var sat = msg.payload[0].col1;
    trim(sat);

    var newMsg = {};
    
    newMsg.payload = {
        sat: msg.payload.sat,
        line1: msg.payload[1].col1,
        line2: msg.payload[2].col1,
    }
    node.send(newMsg);
}
return null;

```

result 3 times  
sat: "SAUDISAT 1C (SO-50)"  
line1: "1 27607U 02058C 25275.89343148 .00004387 00000+0 54779-3 0 9994"  
line2: "2 27607 64.5534 204.4946 0067520 310.0132 49.5059 14.82017782226316"

How do I get this to work but just once per entry

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [3 October 2025 11:54 UTC](https://discourse.nodered.org/t/help-what-am-i-doing-wrong/99251/2 "2025-10-03T11:54:52Z")

</div>

There is a javascript function trimEnd() which should remove only trailing spaces.

Split the payload at new lines, trimEnd() for each line and rejoin.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/optimized/3X/f/2/f2de40f7c182cbf7dac9a78883f8e71127b57491_2_690x59.png)

---

<div class="post-metadata">

### Author: ![Linhey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/linhey/32/73679_2.png) [@Linhey](https://discourse.nodered.org/u/Linhey)
#### Post date: [3 October 2025 11:55 UTC](https://discourse.nodered.org/t/help-what-am-i-doing-wrong/99251/3 "2025-10-03T11:55:11Z")

</div>

AH I have done it.  
It sees the msg.payload length as 3 times, I don't really understand it, but that fixes it

```auto
for (var i = 0; i < (msg.payload).length / 3; i++) {   

```

---

<div class="post-metadata">

### Author: ![Linhey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/linhey/32/73679_2.png) [@Linhey](https://discourse.nodered.org/u/Linhey)
#### Post date: [3 October 2025 12:01 UTC](https://discourse.nodered.org/t/help-what-am-i-doing-wrong/99251/4 "2025-10-03T12:01:59Z")

</div>

Yes I di that first off, but the only way I could get to figure splitting the lines up was to use a csv node, which put each line as col1: _data_ col1: _data_ col1: _data_ that removed the whitespaces from everything including the two data lines, maybe it would work with your suggestion as trim end. I will try that and see.

---

<div class="post-metadata">

### Author: ![Linhey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/linhey/32/73679_2.png) [@Linhey](https://discourse.nodered.org/u/Linhey)
#### Post date: [3 October 2025 12:12 UTC](https://discourse.nodered.org/t/help-what-am-i-doing-wrong/99251/5 "2025-10-03T12:12:28Z")

</div>

Thanks but couldn't get that to work properly, I will stick with my own solution.  
I can't really see why it thinks that each item is a separate message, which it must be doing to triplicate it. anyway I will not worry about it too much.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [3 October 2025 12:29 UTC](https://discourse.nodered.org/t/help-what-am-i-doing-wrong/99251/6 "2025-10-03T12:29:05Z")

</div>

> [@Linhey](#):
>
> I will stick with my own solution.

That's fine, whatever works for you.

I am intrigued though, can you test this flow?

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

```auto
[{"id":"7b2da7bc89bb9f96","type":"inject","z":"36b5ac52249af0ef","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":105,"y":240,"wires":[["2538166138bcfc71"]]},{"id":"2538166138bcfc71","type":"template","z":"36b5ac52249af0ef","name":"","field":"payload","fieldType":"msg","format":"text","syntax":"mustache","template":"SAUDISAT 1C (SO-50) \n1 27607U 02058C 25275.89343148 .00004387 00000+0 54779-3 0 9994\n2 27607 64.5534 204.4946 0067520 310.0132 49.5059 14.82017782226316","output":"str","x":255,"y":240,"wires":[["04a1badac9c37316","06f1737a75e860cd"]]},{"id":"04a1badac9c37316","type":"split","z":"36b5ac52249af0ef","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":160,"y":300,"wires":[["f165be8f695c2292"]]},{"id":"f165be8f695c2292","type":"function","z":"36b5ac52249af0ef","name":"function 2","func":"msg.payload = msg.payload.trimEnd()\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":300,"wires":[["98abe04290a78857"]]},{"id":"98abe04290a78857","type":"join","z":"36b5ac52249af0ef","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":false,"accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":445,"y":300,"wires":[["0cc7a29c3f936b78"]]},{"id":"0cc7a29c3f936b78","type":"debug","z":"36b5ac52249af0ef","name":"After","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":575,"y":300,"wires":[]},{"id":"06f1737a75e860cd","type":"debug","z":"36b5ac52249af0ef","name":"Before","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":410,"y":240,"wires":[]}]

```

For me, Before shows a string of 190 characters, After shows 151 - 39 trailing spaces removed from row 1

---

<div class="post-metadata">

### Author: ![Linhey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/linhey/32/73679_2.png) [@Linhey](https://discourse.nodered.org/u/Linhey)
#### Post date: [3 October 2025 14:50 UTC](https://discourse.nodered.org/t/help-what-am-i-doing-wrong/99251/7 "2025-10-03T14:50:05Z")

</div>

Yes that works fine, it's me.  
It all goes horribly wrong because I split the the whole thing initially with a csv node.  
I did a lot of silly code to get somewhere that was not actually needed.  
Lack of experience and knowledge on my part and there is a much simpler way of doing the whole thing now I look at it again. I thought through this in stages instead of looking at the end game.  
Thanks for your help. I shall look for the wood not the trees next time.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [17 October 2025 14:50 UTC](https://discourse.nodered.org/t/help-what-am-i-doing-wrong/99251/8 "2025-10-17T14:50:25Z")

</div>

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