onBeforeSave syntax failing

I am using example code from the Velo APIs and from other posts on this forum but the syntax never passes:

    $w('#dataset1').onBeforeSave(() => {
        $w("#dataset1").setFieldValues( {
 // Set some hidden values here
        } );
        $w("#dataset1").save();
    });

The parsing error states:
Argument of type ‘() => void’ is not assignable to parameter of type ‘BeforeSaveHandler’. Type ‘void’ is not assignable to type ‘Promise’.ean>'.

Can anyone give a newbie some advice here?

Where do you have this code? Please share more of your code so we can have context of what you are trying to do.

Hi there,

I see a syntax error, the save promise should be outside the onBeforeSave( ) function, putting it inside will create an infinite loop.

The save( ) function triggers the onBeforeSave( ) , so you shouldn’t place it inside it.

Please share the page URL so we can take a deeper look.

Hmm, good point - I missed that. Not sure if it’s a syntax error - looks more semantic.

However, you are correct. onBeforeSave() is triggered by the save() function. Various tasks can be carried out in the onBeforeSave() function, but it certainly doesn’t make sense to call the save() function.

1 Like

@yisrael-wix That’s what I’m saying.

@ahmadnasriya I know. I was just sayin’ it again so I’d sound smart.

@yisrael-wix :joy::joy::joy:

Thanks everyone. Here is the full code. Thing is I am taking this code directly from the Velo documentation and from other examples on the forum. So you are probably correct, the larger code set must be causing this.

import wixUsers from 'wix-users';

$w.onReady(function () {

 // Set member value to logged in email
 // Get logged in user details
    $w("#dataset1").onReady( () => {
 if(wixUsers.currentUser.loggedIn) {    
            wixUsers.currentUser.getEmail()
              .then( (email) => {
                let userEmail = email;      
                $w("#input3").value = userEmail
                $w("#dataset1").setFieldValue('memberEmail', userEmail)
            } );
        }
    } );

    $w("#input1").onCustomValidation( (value, reject) => {
 
 let numVaccines = parseInt($w("#input1").value)
 
 if ((numVaccines%2 == 1)||(numVaccines < 1000))  {
            reject("Vaccines must be ordered in even numbers of doses")
            $w("#text21").text = '$' + (parseInt($w("#input1").value) * 35).toLocaleString('en')
            $w('#text39').show()
        } 
 else {
            $w('#text39').hide()
        }
    });
 
    $w('#dataset1').onBeforeSave(() => {
        $w("#dataset1").setFieldValues( {
 // Set some hidden values here
        } );
        $w("#dataset1").save();
    });

});

Hi Peps,

I am out of resources now. Everything is ok except for this,

I tried four ways

  • 1 copy exact code from velo api
  • 2 put onBeforeSave within Page.onReady
    -3 put onBeforeSave within dataset.onReady
    -4 put onBeforeSave within button.onClick
    and code also work on the live site. It only show the error on my editor.

Some light here please?

Thanks,
DA

The before save function cannot/should not be under any event handler, only include it ONCE under the dataset’s onReady function.

$w.onReady(() => {
    $w('dataset').onReady(() => {
        $w('dataset').onBeforeSave(() => {
            // Add your code here
        })
    })
})
1 Like

Hi @ahmadnasriya

Appreciated the kind info. I tried on a blank page with dataset (write only)on it
and still got the error. Also I tried copying your code just in case but still the same outcome. Done clearing browsing, using incognito and refreshing editor - NO LUCK!

It could be on my side or a bug. This happen when I duplicate my website since the original has an issue (can’t navigate) I have deleted it.

Here are the other errors which was ok in the original website:

Thanks in advance,
DA