I have a loop that works, and within that i'm using "if" to check the difference in days between 2 dates. When the "if" is matched I'm setting some flow variables which are used by the next node to send emails with other flow variable data.
For testing the incoming array has 2 entries and their expiry dates match the 30 day difference. They are correctly matched by the "if" statement.
As below debug output, within the loop the email addresses at array index 0 and 1 are correctly pulled out and the loop counter increments from 0 to 1. However, it appears from the order of the debug output the node.send's don't execute until the loop has finished, and this results in the next node only using the 2nd email address thats stored in the flow variable at that time (same for the other flow variables).
The debug message order I would expect to see is:
emailaddress-1 / loop count: 0 / debug 1 / emailaddress-1 then emailaddress-2 / loop count: 1 / debug 1 / emailaddress-2
I've tried using node.send in different places with no succcess. I also read up a bit on " Asynchronous context access" which might be related to my issue, but couldn't figure it. Bit confused about whether it should be synchronous or asynchronous processing and how to achieve the correct result.
Sure its a school boy error
24/09/2021, 20:21:08node: Check for expiring DPSK & send EMail notification
function : (warn)
"email: emailaddress-1"
24/09/2021, 20:21:08node: Check for expiring DPSK & send EMail notification
function : (warn)
"loop count: 0"
24/09/2021, 20:21:08node: Check for expiring DPSK & send EMail notification
function : (warn)
"email: emailaddress-2"
24/09/2021, 20:21:08node: Check for expiring DPSK & send EMail notification
function : (warn)
"loop count: 1"
24/09/2021, 20:21:08node: debug 1
msg : Object
{ _msgid: "6483b845c5ad0ace" }
24/09/2021, 20:21:08node: debug 1
msg : Object
{ _msgid: "6483b845c5ad0ace" }
24/09/2021, 20:21:08node: Check DPSK Action & Format Notifications
function : (warn)
"email: emailaddress-2"
24/09/2021, 20:21:08node: Check DPSK Action & Format Notifications
function : (warn)
"email: emailaddress-2"
First node:
//create newMsg & initialise variables
var newMsg = {};
var datefromDPSKexpiry = "";
var microSecondsDiff = "";
var daysDiff = "";
var todaysDateTimeStamp = new Date(flow.get("todaysDate")).getTime();
//Set totalCount to number of Active DPSK received in the array
var totalCount = msg.payload.page.totalCount;
//Loop through the received DPSK array and check expiration = 14 days
//If it is then format and send EMail notification
for(counter = 0; counter < (totalCount); counter++) {
flow.set("dpskExpiring", false);
datefromDPSKexpiry = new Date(new Date(msg.payload.contents[counter].expirationDateTime)).getTime();
microSecondsDiff = Math.abs(datefromDPSKexpiry - todaysDateTimeStamp);
// Math.round is used instead of Math.floor to account for certain DST cases
// Number of milliseconds per day = 24 hrs/day * 60 minutes/hour * 60 seconds/minute * 1000 ms/second
daysDiff = Math.round(microSecondsDiff / (1000 * 60 * 60 * 24));
//Check if the current DPSK being checked in the array DPSK will expire in 30 days
//If it does then send EMail notification
if (daysDiff === 30) {
flow.set("dpskExpiring", true)
flow.set("serviceLevel", msg.payload.contents[counter].name);
flow.set("userEmail", msg.payload.contents[counter].thirdPartyId);
flow.set("dpskPassphrase", msg.payload.contents[counter].passphrase);
flow.set("dpskExpiry", msg.payload.contents[counter].expirationDateTime);
node.warn("email: " +flow.get("userEmail"));
node.warn("loop count: " +counter);
node.send(newMsg);
node.done();
}
}
return;
Second Node:
//If DPSK is Expiring soon based on # of days left threshold then format EMail Notification & send
if (flow.get("dpskExpiring") === true) {
node.warn("email: " +flow.get("userEmail"))
newMsg.topic = "Your Wi-Fi Access Key (passphrase) is due to Expire";
newMsg.payload = "<font size = 3>Your Wi-Fi Access Key <B>"+flow.get("dpskPassphrase")+"</B> for the Wi-Fi network <B>"+flow.get("wifiNetwork")+"</B> will expire in 2 weeks time.<BR><BR>The Access Key is valid until <B>"+flow.get("readableExpiryDate")+"</B><BR><BR>Your current Service is: <B>"+flow.get("serviceLevel")+"</B><BR><BR>Please renew your access key by clicking the link below<br><a href='http://10.10.10.10:1880/Login'>Login to Tenant Wi-Fi Access Portal</a>";
return [null,newMsg];
}