šŸš€ RunJS 1.3.2

Hi! Thank you very much for this extension, Iā€™m using it in several server apiā€™s.

I have a small issue and I canā€™t figure out what exactly the cause is.
Iā€™m using a server connect api to calculate results with code in RunJS, Iā€™m requesting the results by sending data to perform the calculations on trough the API Action component in App Connect.

The issue is that 50% of the time RunJS returns no results. I checked the request of all empty results, and they all had the necessary data sent and there were no errors.

What could be happening in the cases where there is no result? Something related to RunJS, my code, the network or server connect?

let multipleWins = false;

let sectorArr = participants;
let prizes = prizesArr;
let winners = [];

function getWinnerChances() {
  let totalChances = 0;
  for (let w = 0; w < winners.length; w++) {
    totalChances = totalChances + winners[w].chance;
  }
  return totalChances;
}

function startGame() {
  for (let prize = 0; prize < prizes.length; prize += 1) {
    let winningSector = randomSector();

      prizes[prize].winner = sectorArr[winningSector - 1].id;
      winners.push({
        winner: sectorArr[winningSector - 1].id,
        prize: prizes[prize].id,
        chance: sectorArr[winningSector - 1].chance,
        coins: prizes[prize].coins,
      });

      if (multipleWins == false) {
        sectorArr[winningSector - 1].chance = 0;
        sectorArr[winningSector - 1].radians = 0;
      }
  }
}

function randomSector() {
  let chancesArr = sectorArr;
  if (multipleWins == false) {
    for (i = 0; i < chancesArr.length; i++) {
      if (chancesArr[i].chance > 0) {
        chancesArr[i].chance =
          chancesArr[i].chance +
          getWinnerChances() / (sectorArr.length - winners.length);
      }
    }
  }

  const cumulativeSum = (
    (sum) => (value) =>
      (sum += value)
  )(0);

  let newList = chancesArr.map((currElement, index) => {
    let max = sectorArr[index].chance;
    currElement = max;
    return currElement;
  });
  let cumulativeList = newList.map(cumulativeSum);

  let listMinMax = cumulativeList.map((currElement, index) => {
    let max = currElement;
    let min = 0;
    if (index == 0) {
      min = 0;
    } else {
      min = cumulativeList[index - 1];
    }
    currElement = { min, max };
    return currElement;
  });

  let cSector = Math.random();

  let chosenSector;

  for (let i = 0; i < listMinMax.length; i++) {
    if (listMinMax[i].min < cSector && listMinMax[i].max > cSector) {
      chosenSector = i + 1;
      break;
    } else {
      continue;
    }
  }

  return chosenSector;
}

startGame();

return winners;


Hard to know.

Have you marked the ā€œShow errorā€ option and checked SC logs?

Ohh sorry, I was checking the browser console and not the SC logsā€¦

Failed to execute script. TypeError: Cannot read properties of undefined (reading 'id')
    at startGame (RunJS:20:59)
    at RunJS:87:1
    at RunJS:89:20
    at Script.runInContext (node:vm:141:12)
    at App.exports.run (/opt/node_app/extensions/server_connect/modules/runjs.js:12:29)
    at App._exec (/opt/node_app/lib/core/app.js:491:57)
    at App._exec (/opt/node_app/lib/core/app.js:458:28)
    at async App.exec (/opt/node_app/lib/core/app.js:427:9)
    at async App.define (/opt/node_app/lib/core/app.js:417:9)

Getting this. So I guess problem with the code
Strange that it works half of the time though

1 Like

BTW I am glad you are finding it useful. Sometimes I would love to know how many people use it :slight_smile:

1 Like

Iā€™m using it in like 7 server connect apiā€™s, really useful. Iā€™m very new to Javascript so making modules would be much more complicated for me, this makes it easy for beginners to do stuff

1 Like

For all of you that are using this extension you might want to vote for this Feature Request:

The actual multiline field is less than ideal for coding :slight_smile:

Problematic function might be random sector

Error occurs in start Game in this line:
prizes[prize].winner = sectorArr[winningSector - 1].id;

Specifically after the equal sign

1 Like

Hi! Like half of the data wasnā€™t formatted correctly so whenever the random pick was in the incorrect half of the data it was throwing the error. Thatā€™s why it was working half of the timešŸ˜… Now with correct data all works. Thanks for taking the time to look trough it

Dose this give us the ability to use npm packages ?

Please please say yes :raised_hands:t5: Lol.

Yes you can.

You can also use node packages with custom extensions.

Could you add an option to actually throw an error? Right now, if thereā€™s a script error, steps proceed anyway, which is undesirable in case of an error

1 Like

Thanks to @sid for the contribution.

2 Likes

Heyaā€¦ I think I did something wrong with the installion:

Installed the both files in /extensions/server_connect/modules/:

ā€¦then added the RunJS action and added some ā€œcodeā€ (btw. I just realized that thereā€™s no ā€œDataā€ table for the params?)

However, on save, the ā€œcodeā€ field gets emptied and a error message appears that the code field is mandatory:

I am pretty sure, I missed one fundamental step somewhere?

/heiko

Eveything seems ok on my end with the extension(using latest 1.3.2)

Try restarting the app.

I did a couple of times, also ā€œreinstalledā€ the files. I just realized this morning that the data table is also missing.

I am using Wappler 5.3.1 on a Mac, if this helps.

Yep. Me too.

Besides re-adding the files and restarting the desktop app, have you tried creating a new Server Connect action with a new RunJS action step?

If it still doesnā€™t work maybe follow this guide and open a bug report so the team can check it as it seems something related to the UI. The Data UI control should be appearing not matter what. If itā€™s not itā€™s probably throwing an error that can be found in the log.

Hi,

Yes, this is a bug. In fact, two separate bugs

The code emptying is because you attempted to save without unfocusing the textarea, so the event responsible for actually updating the code textarea internally is never called. This can be worked around by clicking the mouse somewhere else before attempting to save it. Feel free to open a bug report

Actually, this did the trick: removed the whole action, closed Wappler, removed the files, installed the files again and added the action again, now it looks okay to me.

Thank you.

/heiko

Good point, had a similar issue with renaming properties. One to watch for.

/heiko

In my experience, this bug is limited to just custom extensions. For built-in components, SA is saved correctly even with focus on textarea.

1 Like