I have a simple HTML form where there are 3 text type inputs, one called "Day" another called "Month and another "Year (Year)" , and this same one in a variable called "Date" does the following:
var fecha = dia + '/' + mes + '/' + anio
And the result would be something like: 26/05/2021
, I need to validate that when someone enters this date and a time again (Ejemplo: 08:30 pm)
, an error type
"This appointment already exists, please check for an available one"
The code I am testing is:
else if (fecha == db.collection("citas_registradas").fecha && hora == db.collection("citas_registradas").hora) {
alert('Esta cita ya existe, por favor, verifique una disponible.');
fecha
is the name of my field in my firebase database, and " citas_registradas
" my table, does anyone know how I can do that validation? , I would be very grateful.
Suppose the "time" is entered by the user in a fourth input. And that in the object we save the date and time entered
fecha_input
in its property .fecha_input.date
fecha_input.hora
In the database we save all that data in an object called
fecha
within a documentFirebase
as follows:The next time a user registers an appointment again, what needs to be done now is to validate if that data already exists in the database.
To do that validation, you could do a query
.get()
and use the condition.where()
.For example:
In this example we would be accessing the data in the collection
citas_registradas
and looking for all the documents whose valuefecha.date
andfecha.hora
are equal to the date and time that they just entered in the inputs, which in this case would be in the object calledfecha_input
.The result of that query will be stored in the object
fechaExiste
.Then we do a validation with a
if
:In this way, if there is data found in the object, we
fechaExiste
communicate the message to the user.In this documentation there is more information about this type of queries to
Firebase
:Get multiple documents from a collection
According to the response of my friend Desarrollos-Web-Urquiza, I modified his response to adapt it, it is not necessary to use a constructor or an object or anything like that, so the code that worked for me would be the following:
Substituting Swal.fire for Alert, since Swal.Fire is a CDN to make alerts more visual, attached link: https://sweetalert2.github.io/