Code for querying database

Question:
Can someone help me build out a query for checking if current date is passed/not passed the date in the database?

Product:
Editor X

What are you trying to achieve:

  1. I have a database filled with different access passwords. The database consists of the passwords, a start date and an end date for each code.
  2. Members visits our site and fills inn an access password
  3. The code checks if the password is to be found in the database. If so, it checks if the start date has passed and the end date is not passed (is the code valid)

What have you already tried:
So far I have a working solution for checking if the password is to be found in the database.

import wixData from 'wix-data';
$w . onReady ( function () {

  $w("#feilmelding").hide();

$w ( "#sjekkKode" ). onClick ( event => {
wixData.query("Smartskole")
  .eq("kode", $w("#oppgiKode").value)
 .find()
  .then((results) => {
if(results.items.length > 0)
$w ("#multiState").changeState ("skjema")
;

else ($w("#feilmelding").show())

        }) 
}) 

Additional information:

This code is insecure. If a user can query a collection from their browser they can read the collection entirely and the passwords being stored in it.

I’d recommend reading up on:

import wixData from 'wix-data';
$w.onReady(function () {

            $w("#feilmelding").hide();

            $w("#sjekkKode").onClick(event => {
                wixData.query("Smartskole")
                    .eq("kode", $w("#oppgiKode").value)
                    .find()
                    .then((results) => {
                        if (results.items.length > 0) {
                            $w("#multiState").changeState("skjema");

                            const item = results.items[0];
                            const today = new Date();

                            // check if start date has passed

                            const startTimeDifference = today.getTime() - item.startDate.getTime();
                            const startDateHasPassed = startTimeDifference > 0;

                            // check if end date has not passed

                            const endDateDifference = today.getTime() - item.endDate.getTime();
                            const endDateHasNotPassed = endDateDifference < 0;


                        } else {
                            $w("#feilmelding").show()
                        }

                    })
            })
})
1 Like