I wrote this code, which helps assigning a random number and sending by e-mail to the user who push the button, recording everything in a database.
In preview-mode everything works correctly and the database records the number generated and the user, signing that, from that user, the code was required.
Instead, on the live site, the code works but the database doesn’t record activities.
import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
$w.onReady(function () {
});
export function button1_click(event) {
let discountCode = Math.floor(Math.random() * 8999) + 1000;
let User = wixUsers.currentUser.id;
let toinsert = {
"member": User,
"codiceSconto": discountCode,
"codiceInviato": true
};
wixData.insert('Codicisconto', toinsert)
$w('#dbCodiciSconto').save()
.catch((err) => {
console.log(err);
});
console.log('Inserito nel database')
wixUsers.emailUser('S2A6sCb', User, {
variables: {
codiceSconto: discountCode.toString()
}
})
.then(() => {
console.log('mail inviata')
$w('#button1').disable();
wixLocation.to(`/account/my-account`)
})
}
Could someone check the code to find the problem, please?
If is not a code problem, which could be the real problem?