# Function to replace a word with payload

**URL:** <https://discourse.nodered.org/t/function-to-replace-a-word-with-payload/38547>\
**Category:** General\
**Created:** [4 January 2021 20:09 UTC](https://discourse.nodered.org/t/function-to-replace-a-word-with-payload/38547 "2021-01-04T20:09:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paco1503](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@paco1503](https://discourse.nodered.org/u/paco1503)\
**Post date:** [4 January 2021 20:09 UTC](https://discourse.nodered.org/t/function-to-replace-a-word-with-payload/38547/1 "2021-01-04T20:09:10Z")

</div>

Hey, hope i am using this forum like it used to be.  
At first english is not my first language 😉

I am new to node red and programming.  
I want to replace a word in a msg.payload with coordinates i get from an array.

msg.payload = "[http://api.openweathermap.org/data/2.5/weather?lat=breite&lon=laenge&appid=xyz](http://api.openweathermap.org/data/2.5/weather?lat=breite&lon=laenge&appid=xyz)";  
var url = msg.payload.replace("laenge",msg.payload[0]).replace("breite",msg.payload[1]);  
msg.payload = url  
return msg;

so that he sends the url to a http request but with the coordinates.

If u need more information or something let me know! I didnt find a solution for this problem and with my bad knowledge about programming i am done and need 2 ask for help.

greetz paco

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [4 January 2021 20:13 UTC](https://discourse.nodered.org/t/function-to-replace-a-word-with-payload/38547/2 "2021-01-04T20:13:37Z")

</div>

> [@paco1503](#):
>
> var url = msg.payload.replace("laenge",msg.payload[0]).replace("breite",msg.payload[1]);

msg.payload.replace() is a string function, but msg.payload[0] is an array. They both can not exist at same time. msg.payload is either a string or an array.

---

<div class="post-metadata">

**Author:** ![paco1503](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@paco1503](https://discourse.nodered.org/u/paco1503)\
**Post date:** [4 January 2021 20:16 UTC](https://discourse.nodered.org/t/function-to-replace-a-word-with-payload/38547/3 "2021-01-04T20:16:03Z")

</div>

is there a way i can make it possible otherwise?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [4 January 2021 20:19 UTC](https://discourse.nodered.org/t/function-to-replace-a-word-with-payload/38547/4 "2021-01-04T20:19:38Z")

</div>

sure use a different variable name for the msg.payload

```auto
let url = "http://api.openweathermap.org/data/2.5/weather?lat=breite&lon=laenge&appid=xyz";
url = url.replace("laenge",msg.payload[0]).replace("breite",msg.payload[1]);
msg.payload = url;
return msg;

```

---

<div class="post-metadata">

**Author:** ![paco1503](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@paco1503](https://discourse.nodered.org/u/paco1503)\
**Post date:** [4 January 2021 20:24 UTC](https://discourse.nodered.org/t/function-to-replace-a-word-with-payload/38547/5 "2021-01-04T20:24:20Z")

</div>

you are awesome! have a nice day!

is it possible to explain me what "let" does? or give me an link where i can learn this.  
Just Copy Past is ok for now but not for the future 🙂

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [4 January 2021 20:27 UTC](https://discourse.nodered.org/t/function-to-replace-a-word-with-payload/38547/6 "2021-01-04T20:27:34Z")

</div>

[https://www.w3schools.com/js/js\_let.asp](https://www.w3schools.com/js/js_let.asp)

similar to Var but scope is is different.

---

<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:** [18 January 2021 20:27 UTC](https://discourse.nodered.org/t/function-to-replace-a-word-with-payload/38547/7 "2021-01-18T20:27:37Z")

</div>

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