Substr in function

Hello all,

I thought I could put a substring in a function but it doesn't work a priori, what is my mistake please?

I have this error => TypeError: Data.substr is not a function

Below my (simple) function :

let Data = msg.payload;
let totalLeaks = (Data.substr(6,6));
let lastleakduration = (Data.substr(12,6));
msg.payload = {
    leaks : totalLeaks,
    duration : lastleakduration
}
return msg;

Hi @bpi2008

substr is a function only available on String objects.

This error suggests the msg.payload you are passing this function is not a String.

Can you wire in a Debug node in parallel to see exactly what msg.payload you are passing into the function?

Hi Knolleray,

decoded_payload: object
BAT_V: 3.144
LAST_WATER_LEAK_DURATION: 0
MOD: 2
WATER_LEAK_STATUS: 0
WATER_LEAK_TIMES: 92

I have changed simply my function and i's ok now by :

var duration = msg.payload.uplink_message.decoded_payload.WATER_LEAK_TIMES;
msg.payload = duration;
return msg;

I'm very interesting to test "substr" for other functions.

Many thanks for all. Bernard

For the record, you should be using substring(), substr has been deprecated.

very good thanks Colin

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