Hi,
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) {
wixData.query("counter").find().then((result)=>{
let val = event.target.value;
let count = counter(val);
$w("#playCounter").text = "Total Plays " + counter++;
wixData.update("counter", val).then(()=>{
$w("#dataset2").refresh();
});
}) ;
}
function counter(str) {
var count = $w("#playCounter").text.toString();
}
For starters NaN means not a number.
https://www.w3schools.com/jsref/jsref_number_nan.asp
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.
https://www.w3schools.com/jsref/jsref_tostring_number.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
Plus you are not waiting for your page to get ready and load before the audioplayer ended function.
https://www.wix.com/corvid/reference/$w.html#onReady
https://www.wix.com/corvid/reference/$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.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem
$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.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValue
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…
$w("#myDataset").save()
.then( (item) => {
wixLocation.to("/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?