Random Number in NodeJS

Hi,
I’m trying to obtain a random number taking from a variable and then using it to generate a username with a number random in the end, example “alexander.paladines55”.

In php project I normally use:
<dmx-value id="myNumber" value="<?php echo(rand(100000000,999999999)) ?>"></dmx-value>

Now in NodeJS I’m trying to obtain a random number from javascript:

<script>
  function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1) ) + min;}
    document.getElementById("numberRand").value = getRndInteger(1,99);
</script>

“numbRand” is a text input because I don’t know how to pass it into UI variable. The above works at half.
The script is placed it at end of a content page, only in the page where a actually need it
When I first time open the content page, the random number is generating into input value, but when I refresh the page I get “0” value, like if the script do not found the element to update value. No error on browser console.

How I can pass the result of the script into a UI variable <dmx-value id="var1"></dmx-value>, or if maybe exist another elegant way to generate a random number and place it into a variable or text input at least.

I’m was trying to put it into Static Event with getRndInteger(1,99) but I can’t get it works.

Well, you can parse a value from any JS script into a variable or other element in Wappler using dmx.parse

So you would place this somewhere in your script:

dmx.parse("var_randomNumber.setValue('" + randomNumber+ "')")

And when your script runs, it will set the value in the variable in Wappler.

If you are using content pages and your variable is in a content page, you would need to do:

dmx.parse("content.var_randomNumber.setValue('" + randomNumber+ "')")

Does that help or have I misunderstood?

Naturally, the randomNumber i refer to in the code would be a var that is defined in your JS script.

You could also develop a simple customer module/extension to generate a random number in Server Connect if its something you use more often

I don’t know why but it didn’t work for me. I ended up doing this:

<script type="text/javascript" defer="true">
  function randomNumber() {
    const c = Math.floor(Math.random() * (100 - 1)) + 1;
    document.getElementById("numberRand").value= c;
    }
</script>

And a Static Evento onblur="randomNumber()" when I need it to update.

I usually use a custom query to generate random numbers or alphanumeric values! What DB are you using?

Hi, thanks for answer.
I’m using mysql in node project.

I have this simple inputs:
image

Then the username is autogenerate using these to fields plus the random number:
image

Like I said before I used php into value to generate random number, but in node I only see the chance to generate the number in script and then later capture that number. And this is the only thing that I could make it to works.

I don’t know if there are a elegant way to do it, maybe a custom formatter, but this function is something that will be use in a couple of time, so, something simple in UI will be enought.