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
})
})
})