Hi,
This is my first time using the Atlas App Services, and while working on my app I faced a weird issue with functions which I’m not able to explain & figure out. I’m sure there is an explanation for the behavior, it will be great if someone can explain it.
So here it goes:
The below code is not exactly my app code, but it is sufficient to explain the behavior. I have created a function myFunc
which has 2 nested loops for rows & columns. For every row I get 2 column numbers by calling another function getSetWithNumbers
in the same file. This function returns these 2 numbers in a Set.
const COLS = 6;
const ROWS = 6;
function getSetWithNumbers(start, end) {
const set = new Set();
const num1 = start;
let num2 = start + 2;
if (num2 > end) {
num2 = start + 2 - end;
} else if (num2 === end) {
num2 = 0;
}
set.add(num1);
set.add(num2);
console.log(
`Added ${num1} & ${num2} to the set for row ${start}, set size: ${set.size}`
);
return set;
}
function myFunc(request, response) {
for (let row = 0; row < ROWS; row++) {
const mySet = getSetWithNumbers(row, ROWS);
for (let col = 0; col < COLS; col++) {
if (mySet.has(col)) {
console.log(`Row-${row}: mySet has col ${col}: ${mySet.has(col)}`);
}
}
}
return 'Hello World!';
}
exports = myFunc;
Now here is the weird part:
If I call myFunc using my local node setup I get following logs (everything is as per the expectation):
Added 0 & 2 to the set for row 0, set size: 2
Row-0: mySet has col 0: true
Row-0: mySet has col 2: true
Added 1 & 3 to the set for row 1, set size: 2
Row-1: mySet has col 1: true
Row-1: mySet has col 3: true
Added 2 & 4 to the set for row 2, set size: 2
Row-2: mySet has col 2: true
Row-2: mySet has col 4: true
Added 3 & 5 to the set for row 3, set size: 2
Row-3: mySet has col 3: true
Row-3: mySet has col 5: true
Added 4 & 0 to the set for row 4, set size: 2
Row-4: mySet has col 0: true
Row-4: mySet has col 4: true
Added 5 & 1 to the set for row 5, set size: 2
Row-5: mySet has col 1: true
Row-5: mySet has col 5: true
But when I call this function by pushing it to App Services I get the following logs:
Added 0 & 2 to the set for row 0, set size: 2
Row-0: mySet has col 0: true
Row-0: mySet has col 2: true
Added 1 & 3 to the set for row 1, set size: 2
Row-1: mySet has col 1: true
Added 2 & 4 to the set for row 2, set size: 2
Row-2: mySet has col 2: true
Added 3 & 5 to the set for row 3, set size: 2
Row-3: mySet has col 3: true
Added 4 & 0 to the set for row 4, set size: 2
Row-4: mySet has col 0: true
Row-4: mySet has col 4: true
Added 5 & 1 to the set for row 5, set size: 2
Row-5: mySet has col 5: true
As you can see there are missing entries / logs. What is causing this behavior?
If I replace the Set with a List (with the corresponding changes for add → push & has → includes), everything works perfectly.
This is bugging me for last 3-4 days, so better to have an explanation
Thanks