Random generated text to database

Hello, im triying to send a random generated number in a input to a database. after sending the random code i look into de database but that field is empty.
i have try the code from here https://www.wix.com/velo/forum/community-discussion/solved-i-need-a-help-of-a-wix-expert.
But i dont know how to connect it .

The code is in that input and is sent when is clicked the “vale de comida” button.
Taht my code:

function RandomString() {
 var x = '';
    RandomString = ("BRC"+Math.random().toString(36).substring(2, 16) + Math.random().toString(36).substring(2, 16));
    x = RandomString.toString();
 return x;
}
export function page1_viewportEnter(event) {
    $w('#input19').value = RandomString();
}

that is the code that i have in data.js:


function Vales_beforeInsert(item,code) {
 
 let hook = code;
 
    item.code = (I DONT KNOW RHAT TO PUT HERE) ;
 
 return item;
}

¿How can i connect all to send the random code properly to the database?

https://www.wix.com/velo/reference/wix-data/save

https://www.wix.com/velo/reference/wix-data/insert

https://www.wix.com/velo/reference/wix-data/update

https://www.wix.com/velo/reference/wix-dataset/dataset/setfieldvalue

Can you give me an example of the code ,thanks

You have this code here… when you enter the ViewPort …
…this code-part starts it’s job…

export function page1_viewportEnter(event) {
    $w('#input19').value = RandomString();
}

The “input19” get it’s value (a random String/Number).

Now you want to save this value from “input19” into your desired DATABAS, right?

So, as you can see, there are several, ways how to do it.

Let us take the INSERT()-function.
You will need something like this one (not tested, just an simple example!)

import wixData from 'wix-data';

export function page1_viewportEnter(event) {
 let myVALUE = RandomString()
    $w('#input19').value = myVALUE
    saveData(myVALUE)
}

function saveData(myVALUE){
 let toInsert = {"title":    myVALUE};
    wixData.insert("YourDatabaseNameHere", toInsert)
    .then( (results) => {
 let items = results;
        console.log(items) 


    } )
    .catch( (err) => {
 let errorMsg = err;
    } );
}

Thank you so much, Solved.

Do not forget to like it, if you really liked it :wink: