Deleting a user submission at a specific time

Hi all. I’m very new to coding and trying to learn fast! I’ve got a user submission form that works fine, submits photos and loads to page but also the person submitting the photo chooses a time from a time picker that I’ve created, linked to my DB and is a time field just titled Time. When their submission gets to the time they have input I need their submission to delete or be removed from being visible on the site. It’s not a problem if it stays in the DB, ideally I’d like it deleted from there as well. I’ve also used code taken from @yisrael-wix countdown to convert the user input to a countdown in a repeater. I have no errors in the code but when previewing I’m getting TypeError: Cannot read property ‘compId’ of undefined. and it doesn’t work at all. I’m probably doing something that can’t be done or making a complete hash of the code but at least I’m trying! Here’s the code, any ideas?

import wixWindow from ‘wix-window’;
import wixData from ‘wix-data’;
$w.onReady( function () {

$w('#message').text = ''; 
$w('#group1').hide(); 

**if**  (wixWindow.rendering.renderCycle === 2 || wixWindow.viewMode === 'Preview') { 
    // only when in Front End so we get the user's local time (and not the server time) 
   // otherwise, the display "flashes" - first the server's rendering, and then the local rendering 

   **let**  countDownDate = wixData.get("Timedout","timer"); // Countdown date and time 

   **let**  countdown = setInterval( **function**  () { 
        **let**  now =  **new**  Date().getTime(); 
        **let**  timeLeft = countDownDate - now; 
        **let**  days = Math.floor(timeLeft / (1000 * 60 * 60 * 24)); 
        **let**  hours = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); 
        **let**  minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60)); 
        **let**  seconds = Math.floor((timeLeft % (1000 * 60)) / 1000); 


        $w("#hours").text = "" + hours; 
        $w("#minutes").text = "" + minutes; 
        $w("#seconds").text = "" + seconds; 
        $w('#group1').show(); 

        **if**  (timeLeft > 0) { 
            wixData.remove("Timedout", "_id"); 

        } 
    }, 1000); 
} 

});

Doing this from the frontend with wix-window is not how it should be done What you need is a Job Scheduler, which checks every n time-units if a row in a collection has to be deleted. Velo: Scheduling Recurring Jobs | Help Center | Wix.com

EDIT: daily jobs can only run at a specified time, meaning ONCE per day, making the time rather superfluous. If this is a problem, you could always make an old-fashioned Cron-job out of it, like from easycron.com

Hi Giri. Thanks for the advice, the job scheduler won’t work for what I need unfortunately as the code needs to be checking constantly. I’ll check easycron out. Like a lot of things I haven’t heard of it before, I’m on a VERY steep learning curve! So can this not be done the way I was doing it. Is there no way it can be done using wix code.

Any other advice is greatly received!

The way you started is a dead end, it runs in the front end, so where are all these pages for all users running? Forget it, it´s not how it is done. But you can use Wix Code. You just make a page without any (or very little) output. In it, you check the time (be sure to use UTC) select rows that have a time set lesser or equal to that and delete them. You can test this page manually by just firing it up thru the browser. Now, when it´s working, you go to easycron and you schedule that this job (this Wix Page) has to run every day, every 5 or 10 (or whatever) minutes. That´s it.

Ok, thanks Giri. I’ll have a go at doing it like that, looked at easycron and it should do what I need as well.

Thanks again for the direction and advice!

Glad to help. Just one word of caution: deleting rows is dangerous. If your code has a bug, you might delete the wrong rows. Since Wix db-storage is limitless and performance very good, it might be a better idea to set an “isActive” boolean inside your collection. Then, instead of deleting, you jut set the isActive flag to false. When displaying the data, you just select on isActive= true, so you do not display expired data. Of course, on creation, you set it to true.

Come to think of it, you might bypass the whole cronjob and deleting/setting flags by just filtering the dataset on date/time when displaying information.

Good luck.

I did think about using boolean in my DB but didn’t have the knowledge how to to set it! If I can get it set as you said and then use some code to set it to false at the time the user input that would work. The only thing is that I would still need to check utc against user input future time so not sure I could do that without still using cronjob