I’m developing a feature where I need to dynamically replace parameters in an SQL query based on user input. Each parameter inside $parameter$ must trigger a different Swal.fire() prompt for user input. However, I’m encountering issues where the prompts appear out of order or not as expected. I suspect the problem might be related to how I’m using await within a loop.
Here is the relevant part of my function :
const reemplazarSql = async query => {
// Other parts of the function…
else {
const key = split[i].split(';')[0];
if (filtrosXConsulta.has(nroConsulta)) {
filtrosXConsulta.get(nroConsulta).push(key);
} else {
filtrosXConsulta.set(nroConsulta, [key]);
}
if (!valoresXfiltroPrimario.has(key)) {
console.log(
'valoresxfiltro Primario',
valoresXfiltroPrimario,
valoresXfiltroPrimario.has(key),
filtrosCache.get(key)
);
let valIngresado;
valIngresado = filtrosCache.get(key);
valIngresado = await obtenerDatosUsuario(key, valIngresado);
filtrosCache.set(key, valIngresado);
valoresXfiltroPrimario.set(key, valIngresado);
console.log('ValorIngresado ', valIngresado);
console.log('modificada antes : ', modifiedSql);
modifiedSql += valIngresado;
console.log('modificada despues : ', modifiedSql);
} else {
modifiedSql += valoresXfiltroPrimario.get(key);
console.log('modificada despues : ', modifiedSql);
}
}
};
And here is the obtenerDatosUsuario function:
async function obtenerDatosUsuario(key, valorInicial) {
const { value: resultado, isConfirmed } = await Swal.fire({
title: <span style="font-size: 0.75em; color: black; font-weight: normal;">Ingrese ${key} </span>
,
input: ‘text’,
inputValue: valorInicial || ”,
showCancelButton: true,
confirmButtonColor: ‘#3085d6’,
confirmButtonText: ‘Aceptar’,
cancelButtonText: ‘Cancelar’,
});
if (resultado && isConfirmed) {
console.log(`Resultado ingresado: ${resultado}`);
return resultado;
}
return null;
}
i try different approaches to using async await, such as changing to .then() but i cant solve it yet
Federico Caimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.