How to calulate length of msg.id

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

Can you post an example of what you mean?

Maybe look at JavaScript .length

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

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

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

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.

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


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;
}

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

The first line should be:

var words = msg.id;

error coming
check this

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"

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.

Sir thank you so much now its working.
:slight_smile:

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