onBeforeSave & onAfterSave issues with Lightbox

I created a lightbox popup that would force new members to fill in their user information before proceeding.

I have onBeforeSave in the onReady function that sets the table _id to the user.id and also adds another hidden value.

$w(‘#dataset1’).onBeforeSave(() => {
$w(‘#dataset1’).setFieldValue(‘_id’, user.id);
$w(‘#dataset1’).setFieldValue(‘is_client’, true);
$w(“#dataset1”).save();
});

When I submit the form on the live site, I get the following error in the Javascript console.


I do not get any errors in the Wix development environment.

The same code works in non-lightboxes.

When debugging, I get no errors when $w(“#dataset1”).save(); is removed.

Also, I’m trying to close the lightbox with a onAfterSave.

onAfterSave does seem to work at all on lightboxes.

$w(“#dataset1”).onAfterSave( () => {
wixWindow.lightbox.close(“Profile”);
});

These issues seem to be lightbox specific.

Ideas for workarounds? Or is there something I’m doing wrong or missing.

I’m using Chrome.

You do not have the correct permissions on the Data Collection. Save is not allowed by the User or Visitor (Anyone) on your Data Collection. Right click your Data Collection and set the permission accordingly.

The permissions are correct and some data does get saved. The issue seems to happen when $w(" #dataset1 ").save(); is called. Plus, onAfterSave doesn’t work in my lightbox, but works on dynamic and regular pages. However, onBeforeSave does work.

Hi Brett,

The problem is the .save() call inside your onBeforeSave() function, it is not necessary, the dataset will save after the onBeforeSave() function resolves.

-Lior

Thanks! I’ll test it out. Why the save function then? Also, any idea why onAfterSave does not work in a lightbox? I’m trying to automatically close the light box. Right now, I’m using error detection in onBeforeSave() with a delay. If no errors are detected, I then delay the close, giving enough time for the data to save to the database.

Thanks! Error messaging isn’t showing and all data is now being inserted. Any reason why onAfterSave isn’t working. Thanks again.

@brett82
No problem,
can you please share your page code so we can inspect further?

The page is channelvillebeta

The lightbox Profile is triggered if the user profile has not been completed. The lightbox Profile is the issue.

Currently, I have the following code that closes the lightbox, but it didn’t work so I commented it out.

$w(‘#dataset1’).onAfterSave(() => {
wixWindow.lightbox.close(“Profile”);
});

Instead, I have to close the lightbox in the onBeforeSave with a timeout and some validation to make sure it’s OK to save.

       setTimeout(() => { 
            wixWindow.lightbox.close("Profile")  
        }, 3500);

After seeing and recreating your code, I believe the problem is the fact that you had set the field _id value as the user’s ID

  $w('#dataset1').setFieldValue('_id', user.id); 

The _id field is a unique system field which cannot be edited. even though you are succeeding in changing the values I believe that this is what causing the problems and if you comment-out that line, onAfterSave will work fine.
I suggest inserting the user’s id in a different field in your item schema.

All the Best

  • Lior

UPDATE: After discussing your situation with another team we found that you can change the _id using the onBeforeSave hook and that is an issue on our side.
I will update this thread when there is new information regarding the issue, for the time being, what you can do is to remove the ‘Submit’ action from your button and instead giving it an onClick event with the following code

export function button1_click(event) {
    $w('#dataset1').save()
        .then(() => {
            wixWindow.lightbox.close();
        })
}
  • Lior

Thanks!

Works perfect.

I thought I was going crazy. I spent forever on that issue. It looks like things are changing pretty fast with the platform for the better.

  1. Could you point me to some more examples on save? I swear that I saw an example where I had to use it with setFieldValue.

  2. Is Wix planning to have some more online code seminars with questions from users?

  3. Any interest in user conferences?

Thanks again.

Happy to help!

You can find everything you need in the API or searching the Help Center .
Regarding further educational videos, you can keep up to date in the Wix Tips and Updates Forum

  • Lior

As you can see from my issue, many of us are now pushing the boundaries and both the developers and Wix are learning from each other.

I see great promise from Wix on the coding side and I feel more closer collaboration is not a bad idea.