#scope #function #savedataset #wixdata #textinput #submit #undefined #value #inputvalue
Hello I am having a rather strange problem that may or may not come down to scope, but I have attempted many different ways of troubleshooting this without it working. The issue is that I am using part the current user id to generate a unique code for a user for an affiliate program. The code is then passed into an empty text input element, and then on a button click should be sent to the DB. None of these specific details matter, the main issue I have noticed is that values that are passed to elements on the page by a javascript function will not save to the dataset. If an input’s value is defined outside of a function then it will save to the dataset. The issue there is though that I need a function to create the input’s value. I have tested this with many different ways of writing the code, nesting functions differently, and even making one function asynchronous to no avail. The core thing is that the variables have the correct scope and are being sent the value, this I have logged in the terminal to verify. I have checked all the dataset settings, the page settings, the input setting, tested every option, and again nothing.
Without further adieu here is the code:
import wixUsers from ‘wix-users’;
$w.onReady( function () {
let user = wixUsers.currentUser;
let userId = user.id;
let shortId = userId.substr(0, 5);
var affiliateCode;
function save() {
$w(‘#dataset1’).save();
}
function createCode() {
var name;
var noSpace;
if ($w(‘#input7’).value === ‘’) {
name = $w(‘#input6’).value;
noSpace = name.replace(/\s/g, ‘’);
affiliateCode = noSpace + ‘-’ + shortId;
$w(‘#input8’).value = affiliateCode;
$w(‘#input3’).value = affiliateCode;
$w(‘#text122’).text = affiliateCode;
} **else** {
name = $w('#input7').value;
noSpace = name.replace(/\s/g, '');
affiliateCode = noSpace + '-' + shortId;
$w('#input8').value = affiliateCode;
$w('#input3').value = affiliateCode;
$w('#text122').text = affiliateCode;
}
save();
console.log('Success')
}
$w('#button29').onClick(() => {
$w('#box84').expand();
console.log('Before: ' + ($w('#input8').value = affiliateCode))
createCode()
console.log('After: ' + ($w('#input8').value = affiliateCode))
console.log('Your Code Is: ' + $w('#input8').value)
});
});
In this version of the code I have the console logging the value of the input that is supposed to be submitting the data to the dataset before and after the code creation function is fired. It returns undefined before and returns the correct code after.
Here is a screenshot of the output in the console:
On the page a collapsed box appears and displays the code, this also is working.
But again what is issue is that the code is not being saved to the dataset. The data that the user inputs submits no problem, it’s only the data that is being generated by the createCode() function that will not save.
Please let me know if you can source an issue here as this is driving me insane. Again I spent multiple hours last night trying different ways of nesting the functions and saving the data but to no avail. Thank you.