Database: changing data

Hi, I need simple thing, but I don’t find direct function in Velo to do this:
I want to change specific cell in one row, keeping the rest of data in this row:


One row for one user _id. If the user is new, I would put in new row for him.
Ive tried save() for new user, but this funkcion seems to create another row every time.
Ive tried update(), but console.log doesn’t report anything.
Preview doesn’t show any problems. There are no changes in collection; it totaly doesn’t work when I try like this:

setInterval(function(){ //////////////////it works out of $w.onReady, but setInterval should be called at the end anyway
while ($w("#audioPlayer1").isPlaying===true) {
 let ctime = $w("#audioPlayer1").currentTime; //<----I take data from player, while it's playing
  wixData.query("AudioCT")
       .eq("_id", thisuser)
       .find()
       .then( (res) => {
 if(res.items.length > 0) {
              items = res.items;
              rest1p1 = items[0].t1p1;
              rest1p2 = items[0].t1p2;
 let track =  {
 "_id": thisuser,
 "t1p1": ctime,
 "t1p2": rest1p2 
 }
    wixData.update("AudioCT", track)
    .then( (results) => {
 let checkon = results;
    console.log("Update:"+checkon);
  } )
  .catch( (err) => {
 let errorMsg = err;
  } );
         } else { //<--------------when the user is new
 let track =  {
 "_id": thisuser,
 "t1p1": ctime 
 }
    wixData.save("AudioCT", track)
    .then( (results) => {
 let checkon = results;
    console.log("Update:"+checkon); // <----nothing in console
  } )
  .catch( (err) => {
 let errorMsg = err;
  } );
         }
      })
      .catch( (error) => {
 let errorMsg = error.message;
 let code = error.code;
 } );
}
},3000);

My code looks realy heavy. I don’t believe there is no simple way to place the value simply in correct cell.

The code is a little hard to follow (formatting and indenting). I would suggest that instead of trying to figure out the code with the audio player, try to get the code working with a simpler scenario.

Seems to me that if it’s creating a new row every time, then the query isn’t getting the existing line from the collection. You’ll need to find out what’s happening.

If you still find that you’re having trouble with this, post the URL and explain how and where to see this issue.

Thank you for answer Yisrael. My new code works well in preview, but there is some Permissions problem. In Chrome console shows propper logs. Data is sent, but doesn’t show in AudioCT Collection (live & sandbox checked) even when I’m logged in on site. I expect that setting this function in backend will solve the problem. Now it’s in frontend.

let ctime = Number($w("#audioPlayer1").currentTime);
  wixData.query("AudioCT")
       .eq("_id", thisuser).find()
       .then( (res) => {
 if(res.items.length > 0) { //<- removing previous data for this user, if exists
   wixData.remove("AudioCT", thisuser, options)
  .then( (results) => {
  console.log("Removed");
  let track =  { 
 "_id": thisuser,
 "t1p1": ctime //<- creating variable with user id and value ctime, that I need
 };
 wixData.insert("AudioCT", track, options) //<- new row for this user
.then( () => {
    console.log("Data updated"); 
  } )
.catch( (err) => {
 let errorMsg = err;
  } );
  } )
  .catch( (err) => {
 let errorMsg = err;
  } ); 
  //  } else  ... just insert, if data didn't existed before

This is simple solution, but what if i wanted to keep the rest of columns/informations for this user _id? It changes one value, but in the same time erases the rest of them. I was trying this:

let ctime = Number($w("#audioPlayer1").currentTime);
wixData.query("AudioCT").eq("_id", thisuser).find()
.then((res)=>{if(res.items.length >0){               
items = res.items;               
rest1p1 = items[0].t1p1;               
rest1p2 = items[0].t1p2;
rest1p3 = items[0].t1p3;
let track ={
"_id": thisuser,
"t1p1":  ctime, //variable that I need to change
"t1p2": rest1p2, // this one I kept from previous saving, and that will be removed
"t1p3": rest1p3  
}

…but every change in Collection would trigger the need of editing this code… and what if one of those (ex. "t1p2) is empty? rest1p2 would take value of rest1p3, i think.
I still didn’d find the way to just change the one value in table, I need to change, not touching the rest of data during such operation.

To an update a collection item, you first perform a query for the item to be updated. Then in the returned item object, you modify the relevant field and perform an update() .

@yisrael-wix I’ve done it. Code works well in sandbox/preview. Correct update, correct console.logs. When it goes to live, site reads database, but there are problems with updating and inserting data. I have changed permissions, code is in backend. Probably something’s wrong with code, but how it could work properly in sandbox than?! Should I use some async? But how :slight_smile:
Site adress: https://www.ciezkowice.info/t1

Front-end - something wrong with ctime:

setInterval(function(){ 
let isPlaying = $w("#audioPlayer1").isPlaying; //works well
let ttime = $w("#audioPlayer1").currentTime; //Output: Object object
let ctime = Number(ttime);
console.log("Time taken: "+ctime); //Chrome shows output: "Time taken: NaN"
if (isPlaying===true) {
saveTime(ctime, thisuser, currentPoint);
console.log("Interval"); //Chrome shows this log
}  else {
        wixLocation.to(wixLocation.url);  //refreshes page for other reason
}
},8000);

Back-end, when user is known - totaly doesnt work in Chrome, nothing happens, while in preview works OK:

export function saveTime(ctime, thisuser, currentPoint) {
  console.log(ctime + currentPoint + thisuser); //preview shows this log, Chrome doesn't
 let track;
  wixData.query("AudioCT")
       .eq("_id", thisuser)
       .find()
       .then( (res) => {
 if(res.items.length > 0) {
 let ite = res.items;
 if (currentPoint === "t1p1"){
      track =  Object.assign({}, ite, {t1p1: ctime});
      console.log("Saved:"+track);
    } else {
      track =  Object.assign({}, ite, {t1p2: ctime});
      console.log("Saved:"+track);
    }
wixData.update("AudioCT", track)  
.then( () => {
    console.log("Data updated"); //in preview update works well, no logs in Chrome
  } )

Back-end for new user - creating new row in database:

if (currentPoint === "t1p1"){
  track =  {
   "_id": thisuser, //sent well
   "t1p1": ctime, //in preview OK, in Chrome ctime is not sent: NaN or null. 
   "t1p2": 0   //sent well
 };
wixData.insert("AudioCT", track)
    .then( (results) => {
    console.log("No data, updated"); //in preview proper log, in Chrome nothing
  } )

.currentPoint output supposed to be a number, but it’s Object. When I use Number(), it’s still ‘NaN’ for Chrome.
Documentation sais something differend: https://www.wix.com/velo/reference/$w/videobox/currenttime

setInterval(async function(){ 
let isPlaying = await $w("#audioPlayer1").isPlaying;
if (isPlaying===true) {
 let ctime = await $w("#audioPlayer1").currentTime; //'Object object' in Chrome console, in preview correct 'number' value
    console.log("Time taken: "+ctime);
    saveTime(ctime, thisuser, currentPoint);
    console.log("Interval");
}