Help getting random # on form

Hi all, I am new here and new to trying to pull this off. After looking around I found some code and it looked like I should be able to get this going, but it’s not working. Any ideas on what the problem could be? The $w gives a warning:
Error loading web module backend/data.js: $w is not defined
and I see the total tip saying: Cannot find name ‘$w’.

Any help greatly appreciated, I am also have this input hidden if that makes a difference, with the output being included in the form email.

Thanks,
Jim

export function contact02_beforeInsert(item, code) {
    
    let hook = code;
        
    item.code = randomNumber(10);
    
    return item;
    }
function randomNumber (len) {
    
    var n = '';
    for(var count = 0; count < len; count++) {
        randomNumber = Math.floor(Math.random() * 10);
        n += randomNumber.toString();
        }
    return n;
    }
    $w("#input6") .value = randomNumber (10) ;

Hello,

The function with the $w in it should be in the page code where that element exists.

The hook can stay in data.js as it belongs there.

Hi Amanda, thanks for responding. So the " $w ( “#input6” ). value = randomNumber ( 10 );" should be on the form element on the page? Still getting into Six, can you give me a pointer on the best method to include it at the page level?
Thanks!

Hi, happy to help but I’m not sure exactly what you mean. Do you mean you are not sure what I mean by “page code”?

If that’s the case, this article may help: Where do I put my code?

If this still isn’t clear, let me know. I"m sure we can get y ou moving in the right direction :grinning:

Hi Amanda,
I think I understand where now thank you. I placed " $w ( “#input6” ). value = randomNumber ( 10 ); " on the page code tab that has the form. However I am getting an error of " ReferenceError: Can’t find variable: randomNumber" Do I need to include something else with this code or is the problem in the hook?
Thank you!

Oh, I see. To be able to use the randomNumber function in your page code and also in the data.js file, I would suggest creating the randomNumber function in a jsw file.

That way you can import and use the function in data.js and your page code without having to duplicate code.

This is an article about web modules and how to import to page code, but you can import to data.js as well

Let me know if this is enough information or if anything is still unclear

Hi Amanda, thanks again. I think I have ran into my limits. I’m fairly lost now. I as hoping I was close to achieving this, but seeing as I am not a full fledged dev I am thinking I won’t be able to pull this off now.

Im not even sure what a jsw file is… java script wix? No idea.

You are close, I think you can get there…

First you want to take the random numbers function out of the data.js file and make a new .jsw file.

Put the function in there like this

export function randomNumber (len) {
    
    let n = '';
    for(var count = 0; count < len; count++) {
       let randomNumber = Math.floor(Math.random() * 10);
        n += randomNumber.toString();
        }
    return n;
    }

Then in data.js you can import the function like this:

import { randomNumber }from 'backend/yourFileName'

Which will make it available to use

In your page code, you will import the exact same way as above at the top of your file, then you will be able to use the function there as well

You have already written all the code, it’s just a matter of putting it in the correct locations now! You are close for sure. Let me know if this is helpful.

If you are looking to hire a dev to either mentor or finish up for you, come to our Discord channel and post the request in jobs and collabs

Thanks Amanda! It feels like I’m close… I’ll take a close look at this. appreciate your help.

It is a web module which is a backend file that allows you to export your code for use in other places like your page code, see this article for more details

I think you definitely are, I wouldn’t give up now! Once things click, it will be so much easier for future efforts. The beginning of learning is always the hardest

Hi Amanda, I think I am really close– not working yet but I think the problem is in my code on the home page, if you don’t mind taking one more peak please.

data.js code:

import { randomNumber }from 'backend/numGen.jsw'export function contact02_beforeInsert(item, code) {
    
    let hook = code;
        
    item.code = randomNumber(10);
    
    return item;
    }

numGen.jsw code:


export function randomNumber (len) {
    
    let n = '';
    for(var count = 0; count < len; count++) {
       let randomNumber = Math.floor(Math.random() * 10);
        n += randomNumber.toString();
        }
    return n;
    }

Home page code:

import { randomNumber }from 'backend/numGen.jsw';

$w('#input6') .value = randomNumber(10);

You are very close, the next thing to learn is that when importing from the backend (jsw) to the frontend(page code) is that the backend function will always be a promise, so you need to place this code inside your onReady

randomNumber(10).then((result) =>{
         $w('#input6').value=result
     })

Amanda!!! Thank you it’s working now. Couldn’t have learned this without you. Really appreciate it!

Hi Amanda, I have managed to hit one more snag, when I have this set up with multiple forms, it’s not always working- it works great for me but when I have others test it it doesn’t work on all 3 pages. Really weird. , I am guessing I did the data.js wrong? Maybe I need to see if theres a way to group these into a more condensed version or something?



import { randomNumber }from 'backend/numGen.jsw'

export function enterContest063_beforeInsert(item, code) {
    
    let hook = code;
        
    item.code = randomNumber(6);
    
    return item;
    }


export function enterContest064_beforeInsert(item, code) {
    
    let hook = code;
        
    item.code = randomNumber(6);
    
    return item;
    }
    
export function enterContest06_beforeInsert(item, code) {
    
    let hook = code;
        
    item.code = randomNumber(6);
    
    return item;
    }
Example as set up on one of  the pages:

import { randomNumber }from 'backend/numGen.jsw';

$w.onReady(function () {

randomNumber(6).then((result) =>{
         $w('#input10').value=result
     })
    
});

Thanks for any more advice you can offer, I really appreciate it. I also understand if you are done with me lol.

Thanks!

Jim

Bummer I have had to bail on this since I can’t get it to work consistently when having others testing it. I really wish there was a built in function for something like this.