[Solved] Collections query - current time for many players

I would like to save and use later current time of audio player for each user (to use it in “seek” option). There are more players on differend sides, so I’ve created table with columns: _id=currentuser, t1p1, t1p2, t1p2… for each user and diffrend players. I have two problems:

  1. console.log shows whole table of found result, or “undefined” err. I need just “t1p2” column here. The result: “items” seems to be correct. How to extract exact column from those results?
  2. I have massive problems with stringify. I was trying the seek option for player with local storage. In all scenarios my player doest set properly. After stringify and sending to another let, player still takes the value as an Object. So the question is: how to send the value properly?

let thisuser = (wixUsers.currentUser).id;
console.log( “Id:” +thisuser);
let ctime = $w( “#audioPlayer1” ).currentTime;
let track = {
“_id” : thisuser,
“t1p2” : ctime,
}
wixData.save( “AudioCT” , track);
console.log(ctime);

wixData.query( "AudioCT" ) 
.eq( "_id" , thisuser) 
.find() 

.then( (results) => {
if (results.items.length > 0 ) {
items = results.items; // this looks OK, whole row, exactly the one I need
let t1p2 = items.t1p2; // here may be the problem
console.log(t1p2);
}

 //some code here....
 
 let thisuser = (wixUsers.currentUser).id;
 console.log("Id:"+thisuser);
 let ctime = $w("#audioPlayer1").currentTime;
 console.log(ctime);
 let track =  {
   "_id": thisuser,
   "t1p2": ctime,
 }
    wixData.save("AudioCT", track)
    .then(()=>{
        wixData.query("AudioCT")
       .eq("_id", thisuser)
       .find()
       .then( (results) => {
           if(results.items.length > 0) {
              items = results.items;
              console.log(items)
              let t1p2 = items[0].t1p2; <<<<<<<<---- which data-row exactly? ????
            //if you would use CONSOLE at this code-part, you would get your answer
            //just take a look onto the structure of RESULTS!
              console.log(t1p2);
          }
      });
   });
//continuation of your code here......
   

You already use CONSOLE to inspect your own code. This is a good technique to solve own code-issues. You should perhaps use even more of them!

And do not forget! In JS you will always have to work with PROMISES.
Before you can get some data, you have first to save it (so i implemented a → .then()-function).

I did not test it, but it could work like this for you.

Good luck & happy coding! :wink:

P.S.: Next time please use some CODE-Tags to show your code in a good looking & clean view, like i did it in my example.:sweat_smile:

Thank you russian-dima! The code works well. Console.log shows time, as I needed. Sadly, there is still the second problem making mess: using toString() seemt to help, but at the end something’s wrong when site works:

t1p2 = items[0].t1p2;
        $w("#audioPlayer1").seek(t1p2.toString());

Implementing value into seek doesn’t work. Object type error didn’t show up, but player starts at 0. Possible problem may be here:

The type of column I set up is: number. I don’t find “onTimeUpdated” type on the list. Looks, that player doest recognize the value which comes back from AudioCT.
Test with value written straight to the code: .seek(10.00), goes well.
Maybe there is some problem with saving data?

let ctime = $w("#audioPlayer1").currentTime;
 let track =  {     //deletes previous entry, including t1p1 :(
 "_id": thisuser,
 "t1p2": ctime, 
 }
    wixData.save("AudioCT", track)

You have to find out the right type of value first…

console.log(typeof t1p2)

Then convert the value to your desired value-type to fit it into your database.

Number(value)
String(value)
value.toString()

and so on…

And do not forget —> use CONSOLE —> console.log(typeof t1p2)

1 Like

The type is Number. Thank you for help. This code reads database and sets the start-time correctly:

wixData.query("AudioCT")
       .eq("_id", thisuser)
       .find()
       .then( (results) => {
 if(results.items.length > 0) {
              items = results.items;
              t1p2 = items[0].t1p2;
       }
        $w("#audioPlayer1").seek(t1p2);
              console.log("Time:"+ t1p2);
        $w("#audioPlayer1").show("fade");
        $w("#audioPlayer1").play();
 
      })
      .catch( (error) => {
 let errorMsg = error.message;
 let code = error.code;
 } );

No problem.
Glad i could help you.

Good luck and happy coding.