How to create an empty value in a collection when new member registers

Hi, I would like to ask you for help with coding.

I have a collection of members’ inputs. I would need to create an empty record/row when a new member registers. It can be done via code.

I have no coding knowledge. Could please create the code for me. Thank you very much.

Here is a scenario:

  1. a new members registers

  2. a new record is created in the collection with empty values

  3. I will display the empty record on a page with “Read and Write Mode”.

Thank you again for your assistance. Jan

The question is not clear enough.
When you say “an empty record” do you mean a recored wit only system values such as ID _createdDate, _updatedDate etc… ?

  • are you using custom registration?

Thank you for the additional questions that help specify my needs.

Yes. “an empty record” = record with “only system values”.

I am NOT using custom registration.

I have no idea what’s the point in having an empty row, but I’ll guess you have a reason.

So…
Since there’s no onRegistration event listener, you can detect once a user got registered.
But you can put something like this on the masterPage code

//masterPage.js
import "wixUsers" from "wix-users";
import wixData from "wix-data";
wixUsers.onLogin(user => {
wixData.insert("MyEmptyCollectionName", {_id: user.id});
})

This code will add a record to the collection on login. The record id will be equal to the user id.
If the member has already registered in the past and has an empty record it’ll fail inserting the same record with the same id (and that’s what you want - 1 record per member).

Hi, just an updaate. This code should do it in case somebody needs it. I hired a coder to do that. But I cannot say if the code is ok. It would be good if somebody could “proofread” that.

// API Reference: Introduction - Velo API Reference - Wix.com
// “Hello, World!” Example: Velo Learning Center
import wixData from ‘wix-data’ ;
$w . onReady ( function () {
// Write your JavaScript here

// To select an element by ID use: $w(“#elementID”)

// Click “Preview” to run your code
});

/**

  • Adds an event handler that runs when the element is clicked.
  • @param {$w.MouseEvent} event
    */
    export function button11_click ( event ) {
    let toInsert = {
    “email” : $w ( “#input3” ). value
    };
    wixData . query ( “beneficiary” ). eq ( “email” , $w ( “#input3” ). value )
    . find ()
    . then ( ( results ) => {
    console . log ( results )
    if ( results . items . length == 0 ) {
    wixData . insert ( “beneficiary” , toInsert )
    . then ( ( results ) => {
    let item = results ; //see item below
    } )
    . catch ( ( err ) => {
    let errorMsg = err ;
    } );
    }
    })

This code should work fine. Yet, I’d go for my suggestion above, since it’s shorter and covers some extremely rare edge cases (i.e. when 2 users used the same email at the the exact same time).

@jonatandor35 It did not work. Here is a comment from Wix Support why it did not work. Please have a look.

can see that you would like to insert a new entry in the database once a new member is registered. Upon having a closer look at the code in question, I can see that you are trying to use insert() function on the dataset, instead of the database, which is not going to work as expected.

Besides, you are attempting to create a new entry inside the onLogin() function, which would create a new item every time the user logs in. In the end, you might end up with hundreds of items from the same user, as many times as they have logged in.

If you want to create a new entry immediately after the user is registered, you should create a Velo sign-up form, instead of using a custom one, register the users using register() function, and use the insert(“My database ID”, {}) function to create new entry in the database. The code would not work with the current custom form, since the native registration flow is set to close the lightbox after the form is submitted, and the rest of the code would not be executed.

Otherwise, if you do not want to modify the sign up settings, you can alter the code in the masterPage.js file a bit.

Specifically, you should do the following:

  1. Inside the onLogin() event, find the item in the database (using query() function), which _owner field matches the currently logged-in user ID (using .eq() query builder). This step is necessary to avoid duplicated entries from the same user;
  2. Using the if-statement, insert(“My database ID”, {}) the new empty item in the database if the returned items array length is 0 (0 length means that there are no items that match the filter).

The final code can look similar to this:

@jonatandor35 If you know how to do it better. I would welcome your advice.

Plus I had to use “Custom Sign up Form” and I would prefer to use the “Default Form”.