Hi everyone,
I’m facing an issue where a formula for validating Brazilian postal codes (CEP) using the ViaCEP API is returning undefined
in the WeWeb interface, even though the validation logic works correctly as evidenced by the console logs.
The formula is set up to return true
for valid CEPs and false
for invalid ones. Below, I’ve included the code, console logs, and a screenshot of the setup in WeWeb for context.
Code:
return (async () => {
console.log("Starting CEP validation.");
// Check if the value is provided and not empty
if (!value || value.trim() === "") {
console.log("Error: No CEP provided or it is empty.");
return false; // Return false if the value is empty or missing
}
// Remove non-numeric characters
const cep = value.replace(/[^\d]+/g, '');
console.log(`CEP after removing non-numeric characters: ${cep}`);
// Validate CEP format (must be 8 digits)
const cepRegex = /^[0-9]{8}$/;
if (!cepRegex.test(cep)) {
console.log("Error: Invalid CEP format (must be 8 digits).");
return false; // Return false if the format is invalid
}
const apiUrl = `https://viacep.com.br/ws/${cep}/json/`;
console.log(`Querying ViaCEP API for CEP: ${cep}`);
try {
// Fetch the data from the API
const response = await fetch(apiUrl);
if (!response.ok) {
console.log("Error: Unable to query the ViaCEP API.");
return false;
}
const data = await response.json();
if (data.erro) {
console.log("Error: CEP not found in the ViaCEP database.");
return false;
}
console.log("Valid CEP found in the ViaCEP API:", data);
return true; // Return true if the CEP is valid and found
} catch (error) {
console.error("Unexpected error during CEP validation:", error);
return false; // Return false in case of any errors
}
})();
Console Logs:
Here are the console outputs for a valid CEP (01153000
):
Starting CEP validation.
CEP after removing non-numeric characters: 01153000
Querying ViaCEP API for CEP: 01153000
Valid CEP found in the ViaCEP API:
{cep: '01153-000', logradouro: 'Rua Vitorino Carmilo', complemento: '', unidade: '', bairro: 'Barra Funda', …}
bairro: "Barra Funda"
cep: "01153-000"
complemento: ""
ddd: "11"
estado: "São Paulo"
gia: "1004"
ibge: "3550308"
localidade: "São Paulo"
logradouro: "Rua Vitorino Carmilo"
regiao: "Sudeste"
siafi: "7107"
uf: "SP"
unidade: ""
Issue:
Although the console clearly shows the validation is working (with correct logging at each step), the formula returns undefined
in WeWeb. This makes the validation logic unusable within my project.
Setup Context:
Attached is a screenshot of the formula setup within the WeWeb interface. The formula is saved as a global formula under the “Validation” folder and is linked to a field in the form.
Questions:
- Why does the formula return
undefined
in the WeWeb interface while the console logs the expected output? - Is there a specific requirement for handling asynchronous functions in WeWeb formulas that I might be missing?
- Are there better practices for making async API calls in WeWeb to ensure consistent results?