Help with updating a database

I have a database that collects certain information from a user when they fill out a registration form using a custom registration form. I also have code that will create a unique referral code based on the users ID after they register. I need to update my database for that user with the generated referral code when they submit the registration form.

As you can see this poses a Chicken/Egg problem. Until the user completes registration and wix assigns the user ID, my code can’t generate the referral code so how do i add the needed information to the database when i don’t have the information until after they register?

  1. Register the user

  2. Then let them fill out the form

OK, that makes sense, but then how do i update their existing record as it has to update based on their user ID? I have been trying to work with the toUpdate but nothing i do seems to work. Any insights how this is accomplished. I know it can be done as i have seen many other posts that are similar but most of those solutions are very specific to their site.

can anyone help with this?

When the user is registered, they then log in. Once they are logged in use code before to get their userId.

import wixUsers from ‘wix-users’;

// …

let user = wixUsers.currentUser;

let userId = user.id;
Console.log(userId);

Hi,
I guess this article will be useful for you.
Accomplishing the guide will give you same user.id and _id in the database collection.
After the user in registered and signed in. you can get his user.id from $w(‘#yourDataSet’).getCurrentItem()._id and use wixData.update() method.
Good luck!
Roi.