So, I see that others have had a similar problem and it does not look like anyone has solved it based on my research on the topic. It may just be a bug that has not yet been resolved.
I have a simple form that I have created with user input boxes and a submit button. I used the Connect to Data icon to connect each element to the same dataset.
But here’s the issue…I prepopulated three of the elements with user email, first, and last names.
When I use the button to submit the form to the dataset, only the fields that have been entered by hand are being saved…none of the prepopulated fields save to the database.
Hi,
If an element is connected to a dataset, setting the element’s value in code does not set the value of the connected field in the dataset. That means if you use the dataset to perform a submit, the value changed in code is not reflected in the submitted item.
To submit the new value using a dataset, set the field’s value using the setFieldValue() function before performing the submit.
Check out the API here .
Thank you for the comment. I think I am already setting the element’s value. Here is the code I am using on the page load:
wixData.query('ClientProfile')
.eq("email", userEmail)
.find() // Run the query
.then(results => {
// Set the table data to be the results of the query
let firstName = results.items[0].firstName;
let lastName = results.items[0].lastName;
let clientEmail = results.items[0].email;
$w("#input6").value = firstName;
$w("#input7").value = lastName;
$w("#input3").value = clientEmail;
})
Do the last three lines set the field value for submission to the dataset?
Here is the full code I am using in conjunction with the Connect to Data element icons:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
$w.onReady(function () {
let user = wixUsers.currentUser;
let userId = user.id; // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true
user.getEmail()
.then((email) => {
let userEmail = email;
wixData.query('ClientProfile')
.eq("email", userEmail)
.find() // Run the query
.then(results => {
// Set the table data to be the results of the query
let firstName = results.items[0].firstName;
let lastName = results.items[0].lastName;
let clientEmail = results.items[0].email;
// Set the elements to display the query results and prepopulate three fields
$w("#input6").value = firstName;
$w("#input7").value = lastName;
$w("#input3").value = clientEmail;
$w("#input8").value = clientEmail;
// Set the field values of the dataset
$w("#dataset2").onReady(() => {
$w("#dataset2").setFieldValues({
"first": firstName,
"last": lastName,
"email": clientEmail
});
});
});
});
});