# How to calulate length of msg.id

**URL:** <https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684>\
**Category:** General\
**Created:** [10 September 2020 10:16 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684 "2020-09-10T10:16:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [10 September 2020 10:16 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/1 "2020-09-10T10:16:26Z")

</div>

How to calculate length of msg.id .I want to differentiate between fob id and tag is which are of different sizes .please suggest?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [10 September 2020 10:45 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/2 "2020-09-10T10:45:33Z")

</div>

Can you post an example of what you mean?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [10 September 2020 10:48 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/3 "2020-09-10T10:48:03Z")

</div>

Maybe look at JavaScript .length

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [10 September 2020 11:17 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/4 "2020-09-10T11:17:00Z")

</div>

if(length of msg.id==4)  
{  
do something...  
}  
if(length of msg.id==8)  
{  
do something.....  
}

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [10 September 2020 11:27 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/5 "2020-09-10T11:27:31Z")

</div>

Yes sir i tried this but no output whats wrong in this please answer

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 September 2020 11:55 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/6 "2020-09-10T11:55:45Z")

</div>

Hi @shipra

first we need to understand exactly what type of value you have in `msg.id`. If you pass it to a Debug node, configured to display `msg.id`, that will show us what you have got.

If it is a String type of value, then you can use the length function on the String object - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/String/length](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length)

So in this instance, you would get the length of `msg.id` with `msg.id.length()`.

If `msg.id` is not a String, but is actually some other type, then it depends on what that type is and what you want from it.

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [10 September 2020 12:18 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/7 "2020-09-10T12:18:17Z")

</div>

Sir i have tried this ,please suggest whats wrong in this  
msg.id is a string

```auto

var words='${msg.id}';
var i= words.length;
if(i==4)
{
let newMsg1 = {};

newMsg1.topic = `INSERT INTO NCS_DATABASE3 (Date_and_Time_India,Date_and_Time_UK ,Event_Type ,Event_Description ,Person_ID,Person_Name_Description ,Fob_Tag_ID ,Trade_ID,Door_ID ,Door_Description ,Status)
VALUES ((strftime('%Y-%m-%d %H:%M:%S','now','localtime')),(strftime('%Y-%m-%d %H:%M:%S','now','localtime','-4 hour','-30 minutes')),' ',' ',' ','Shipra Nigam',' ','${msg.id}',' ',' ','DR1 ON')`;
return newMsg1;

}
if(i==8)
{
    let newMsg2 = {};

newMsg2.topic = `INSERT INTO NCS_DATABASE3 (Date_and_Time_India,Date_and_Time_UK ,Event_Type ,Event_Description ,Person_ID,Person_Name_Description ,Fob_Tag_ID ,Trade_ID,Door_ID ,Door_Description ,Status)
VALUES ((strftime('%Y-%m-%d %H:%M:%S','now','localtime')),(strftime('%Y-%m-%d %H:%M:%S','now','localtime','-4 hour','-30 minutes')),' ',' ',' ','Shipra Nigam','${msg.id}',' ',' ',' ','DR1 ON')`;
return newMsg2;
}

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 September 2020 12:22 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/8 "2020-09-10T12:22:26Z")

</div>

> [@shipra](#):
>
> `var words='${msg.id}';`

Without knowing anything about what you want the code to do, I can only look for JavaScript mistakes.

The first line should be:

```auto
var words = msg.id;

```

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [10 September 2020 12:34 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/9 "2020-09-10T12:34:52Z")

</div>

error coming  
check this

```auto
SELECT Fob_Tag_ID FROM NCS_DATABASE3 WHERE Fob_Tag_ID='04222436' : msg : Object
object
topic: "SELECT Fob_Tag_ID FROM NCS_DATABASE3 WHERE Fob_Tag_ID='04222436'"
_msgid: "ca43b588.7a0808"
payload: "CR_SUCCESS"
9/10/2020, 6:01:53 PMnode: a4cb6fc0.fed9function : (error)
"TypeError: words.length is not a function"

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 September 2020 12:59 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/10 "2020-09-10T12:59:37Z")

</div>

If `words.length` is not a function (which is what the error tells you), then `words` is not a String type of object - so therefore `msg.id` is not a String.

So I go back to my original reply - you need to confirm what type of value `msg.id` is. Pass it to a Debug node (configured to show `msg.id`) and then show us what it reports when you pass a message to it.

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [10 September 2020 13:11 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/11 "2020-09-10T13:11:42Z")

</div>

Sir thank you so much now its working.  
🙂

---

<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:** [24 September 2020 13:11 UTC](https://discourse.nodered.org/t/how-to-calulate-length-of-msg-id/32684/12 "2020-09-24T13:11:56Z")

</div>

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