Question:
How do I create a page which allows a member to ADD OR EDIT their data stored in a collection?
Product:
Wix Studio Editor
What are you trying to achieve:
On my website, there is a Signup button that takes the user to a Detailed Profile Page in the Members area. Since they aren’t signed in (and aren’t even a Member yet), they’ll go through the normal Wix Member Sign Up flow.
After signing up, they get to the Detailed Profile Page where they can add additional information about themselves that is stored in a MoreMemberInfo collection (and not in their Member Profile) with their _ownerID. I can do this completely without code EXCEPT that the fields in the form connected to a Read/Write Dataset for MoreMemberInfo are grayed out if the data for that member does not yet exist in the collection.
One no-code way to do this is to create a separate NewMember page and an UpdateMember page, but then I’m essentially duplicating a page and need to update both whenever I change the collection fields. And the user is hosed if they don’t complete the NewMember page and later go to the UpdateMember page (for this case I could check to see if they have a record yet and if not then redirect them to the NewMember page but ugh).
The coder in me wants to write a tiny piece of code on the Detailed Profile Page that onReady sees if the user has a record in the MoreMemberInfo collection and if it doesn’t than simply Insert or Save one and have the form work. The page, or at least Dataset, might need to be refreshed to see the new data? I tried to code that and failed miserably.
Another coding option might be to make sure the record is added whenever a user signs up to be a member. I might be able to code this into a custom Sign Up page but I’m not sure their _ownerID is set up yet?
Is there an easy way to do this? TYIA.
One solution I tried:
Once onReady, check is count===0. If so, do an Insert or Save and then Refresh the dataset. However, I ended up with anywhere from 1 - 20 records inserted when I did this. I think I was close but simply needed to wait for the Insert promise to be fulfilled and then do the Refresh. As originally written, the refresh happened before the onset so it did another insert, and kept doing them until 1 was finally inserted.
I’ll post the code once I’m near a computer. I THINK this is a really simple way to get this functionality (one page for new and update collection).
I couldn’t get the above solution to work. Here’s the summary and my current (a touch lame) plan:
GOAL
One Update page which creates a new record if one didn’t exist or allows user to update the existing record.
APPROACH TRIED
Plan was to have the page with Dataset set to Read/Write. In page onReady, check to see if there is an existing record in the Data. If there isn’t, Insert it and then Refresh the Dataset (hoped that would do the right thing to the page) or refresh the entire page by setting URL.
In general this worked but the page got into a state where the Submit button wouldn’t work. I may debug this further to figure out exactly what is going wrong but in the meantime:
CURRENT SOLUTION
On the main Update page, have code which checks to see if a record exists. If it doesn’t, set URL to NewMember page with Dataset set to Write. User enters one basic field (I don’t want to recreate the entire Update page). The Submit button then jumps back to the Update page.
I think the Insert after Submit will finish before the redirect to the update since I have a “Success after Submit” message that is getting displayed before the redirect but I’ll need to watch this.
I really don’t like having the second page but if that’s what it takes, I’ll do it.