I am calling some data from a Firebase database , the code:
db.collection("citas_registradas").where("fecha", "==", fecha).where("hora", "==", hora)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
var fechaValidacion = fecha;
var horaValidacion = hora;
});
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
And my validations :
if (fecha == fechaValidacion && hora == horaValidacion) {
Swal.fire("Oops!", "La cita ya está llena, por favor, busque una hora libre y asigne la cita.", "error");
}
else if (fecha != fechaValidacion && hora != horaValidacion || fecha == fechaValidacion && hora != horaValidacion) {
Swal.fire("¡Genial!", "La cita está disponible.", "success");
}
But when testing my app, and entering a date and time that is already registered, it gives me the following error:
"dateValidation" is undefined
Is there a way to send those variables that are used only inside the db.collection to a "global" variable to store the data , or a way to define it ? , From already thank you very much.
Your variable is there
undefined
because it is within a local scope, which in this case would bethen
if you want to access that variable from a global scope, you won't be able to because it is only available within that scope and the more nested scopes, I don't know if I can explain myself. Put your conditional insidethen
and that's it}
If you declare them like this, they would be global, accessible to the entire field. If you want to keep them private, you could put the validation inside a function and pass the variables as parameters.