Music listening counter

I am trying to count the number of times user listened to a playback, and to save the number to user input in the “counter” field of the DB collection with read and write permission, but I get no value whatever I do. Without DB connection I get “NAN” value in the string or input field. What am I doing wrong? (I’m only the beginner with corvid)

import wixData from 'wix-data';

export function audioPlayer1_ended(event) {
	let val =;
        let count = counter(val);
        $w("#playCounter").text = "Total Plays " + counter++;
    wixData.update("counter", val).then(()=>{
	}) ;
function counter(str) { 
    var count = $w("#playCounter").text.toString();

For starters NaN means not a number.

Are you storing your dataset field for this value as number of text, if it number then you will need to add toString to make it a text string for your text box on your page.

Plus you are not waiting for your page to get ready and load before the audioplayer ended function.$w.html#onReady$w.AudioPlayer.html#onEnded

If you do want this function to be performed when the page is loaded then you will need to write your code into the pages onReady function itself, which examples can be found for in the Wix API Reference links added below.

With getting the dataset field value, you can just use getCurrentItem to call that from your dataset.
$w(“#myDataset”).getCurrentItem(); - gets all the items data.
$w(‘#dynamicDataset’).getCurrentItem().item - gets just the appropriate field

Also, if you want to resave the new total then you should be using setFieldValue function with the save function afterwards.

You might have to refresh your page for it to show any updated dataset settings, so you can try doing something like this after your save…

.then( (item) => {"/yourpageURL");
 } )
 .catch( (err) => { let errMsg = err; 
 } );

You might want to vote for this feature too.

1 Like

Thanks for the detailed answer!!!
Now I understood the logic of the whole process and will try to double-check the fields properties and rewrite everything. (Voted for a new feature)

But if there is no need to keep the numbers of plays in Database, is there a simple way to add a number after the playback is ended?