Hello,
I’m creating a site for recording nature sightings. I have a page for members only, through which they enter records which populate a collection. Currently, when a user submits a record, they are taken back to the original form with blank fields.
I’m desperately trying to work out how to pre-fill several of the fields with the values from the previous record they submitted. The form works fine as it is, but without pre-filled fields it’s pretty much useless, as entering 100 records would take many times longer. It would also have to be pre-filled with the last of the user’s own records, rather than the last in the database, in case more than one person is inputting at any time.
Any help with this would be very much appreciated. I’ve done pretty much the whole thing without having to add code, but I realise this is going to require extra code. My experience of coding is so outdated that it’s effectively zero, so I’d need any help to be super simplified!
Thank you in advance!
At the moment I’m just trying to pre-fill one input (site), and I’ll add the rest when that works. This is the code I have so far:
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
$w.onReady( function () {
if (isLoggedIn) {
wixData.query(“Website_Records”)
.descending(“_createdDate”)
.eq(“owner”, userId)
.find()
.then( (results) => {
let firstItem = results.items[0]; //see item below
$w(‘#input5’).value = firstItem.site;
} )
}
});
I’m getting the following error message, which comes up for every field I’ve tried: Unhandled promise rejection TypeError: undefined is not an object (evaluating ‘firstItem.site’)
When I use the form on the site, the field (#input5) is just blank. I’m guessing I’m missing something obvious, but I can’t see what I’m doing wrong!
Thanks
The field #input1 is probably blank because firstItem.site is undefined. Is site a field in your datase collection? If so, does it have data in it or is it blank?
You will need to figure out what’s happening here. You can try adding console.log() statements in different places in your code to inspect results/fields/variables/etc. For example:
let firstItem = results.items[0]; //see item below
console.log(firstItem);
The console.log() statement will display the contents of firstItem in the Developers console. You will be able to see all of the fields and their valuse. See the article Testing and Debugging Code with Wix Code for more information.
I hope this helps,
Yisrael
Thanks for your reply. Yes, ‘site’ is a field in my collection (Website_Records) and every record has an entry for that field. I’ve also tried filling the input with other fields, but they don’t work either so the issue is presumably not specific to the field.
Adding the console.log statement doesn’t seem to return anything to the developer console - I had also tried that previously but assumed I wasn’t doing it correctly when it returned no data. I have all 4 options selected for viewing in the console (Debug, Info, Warning, Error).
Could it be something to do with the general settings for the input box? For example, should ‘show text on load’ be set to “none”, or should those settings be overwritten by my code?
Also, if I ever do get it to work(!), should my code be put inside an onClick rather than onReady, so that some fields are pre-filled and the next record can be submitted? i.e. when a record is submitted through the form, does that effectively reload the onReady code and start again?
I hope that makes sense!
EDIT: Just to clarify, if I insert the following three statements into my code, together or individually…
console.log(‘test’);
console.log(items[0]);
console.log(firstItem.site);
…the developer console returns ‘test’ and ‘undefined’ for the first two respectively, and nothing for the third.
Rory
Please post the URL of your site so I can take a peek. Only authorized Wix personnel can get access to your site in the editor.
It doesn’t matter to me whether the fields are pre-filled when the page is first loaded (in fact it might be better if they’re not) but once the first record is submitted, I’d need the fields for ‘Record Type’, ‘Observer’, ‘Site’ and ‘Region’ all pre-filled.
Thanks
Just with a quick look I see that your dataset is configured as Write-only :
The prevents the form from prefilling fields from the database (since it can’t read).
Hi Yisrael,
I did try changing that to read/write previously, but the result was that all fields were pre-filled with the previous record, rather than just the one(s) I’d specified. Also, when I ‘submit’ the record it just changes the values in the previous record, rather than adding a new record to the database. I’ve just tried this again and got the same result.
I’ve tried deleting my code completely, and in read/write mode it does exactly the same thing, so it’s presumably ignoring my code altogether!
Thanks
If the fields are connected to the dataset then you’re right, the code probably doesn’t have much to do.
Since the fields all prefill when set to Read-Write, you’ll have to set it back to Write only. Then do a separate query to get the fields you want, and use the dataset’s setFieldValue() function to “prefill” the desired fields.