Having trouble with looping through a function

I'm not a coder, so bear with me. I trying to create a flow that pulls info from an API call, checks to see if a sms message has come in in the last 5 min, and then moves it on down the flow.

I have the function working well enough, but I have tried all day to figure out how to get it to iterate through the array.

If the array entry's date does deem to be less than 5 min ago, at minimum I need payload.sms[XX].message and payload.sms[XX].contact.

I don't have a foreach function working well enough, so I won't bother sharing it.

Hard coded Index Function that works:

var Data = msg.payload.sms[0].date;
var NRtime = msg.time;

//Take Date and move to epoch
var myDate = new Date(msg.payload.sms[0].date);
var sms_time = myDate.getTime();

msg.sms_time = sms_time;

//How many minutes ago? 
//2min = 120000
//5min = 300000
var Xminago = NRtime - 300000;

//check to see if sms is between times
if (sms_time > Xminago && sms_time < NRtime) {
  msg.check="TRUE";
  return [msg, null];
} else {
  msg.check="FALSE";
    var msg1 = {payload: "Time is outside these values"};
    return [null, msg1];
}

Payload being fed to function:

{"status":"success",
 "sms":
      [{"id":"59229728",
        "date":"2022-08-04 07:41:02",
        "type":"1",
        "did":"XXXXXXXXXX",
        "contact":"XXXXXXXXXX",
        "message":"8/4/2022 7:40 AM Testing continues",
        "col_media1":"",
        "col_media2":"",
        "col_media3":"",
        "media":[]
		},
        {"id":"59230486",
         "date":"2022-08-04 08:23:15",
         "type":"1",
         "did":"XXXXXXXXXX",
         "contact":"XXXXXXXXXX",
         "message":"8/4/2022 Checking index",
         "col_media1":"",
         "col_media2":"",
         "col_media3":"",
         "media":[]
	    }
	  ]
}

Hope it helps

// set data use const as no need for a global and data will always be an array
const Data = msg.payload.sms;
// set time now leave blank, added date for testing, returns timestamp in milliseconds
const NRtime = new Date("2022-08-04 08:26:15").valueOf(); 
// loop through array, each object is now in obj
Data.forEach(obj =>{
    // get timestamp of the smms message, used let as the variable will not always be  a object
    let myDate = new Date(obj.date).valueOf();
    msg.sms_time = myDate;
    // get time diff between object time and time now, abs sets it to positive number
    const time_dif = Math.abs(NRtime-myDate);
    // for testing to see data
    node.warn(NRtime + "\n" + myDate + "\n" + time_dif)
    if (time_dif < 300000){
        msg.check="TRUE";
        //  set payload to the obj in the current loop
        msg.payload  = obj;
        // send msg as in loop can not use return
        node.send([msg, null]);
    } else {
        msg.check="FALSE";
        const msg1 = {payload: "Time is outside these values"};
        node.send([null, msg1]);
    }
})
//return nothiing can be omitted
return null;

That works great. I'm still combing through it to see what I can learn.

THANKS!

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