#AlienSoft
1 messages · Page 1 of 1 (latest)
Can you provide some more context?
so on my: async function handleSubmit(e) {
i do some checks and where it's supposed stop executing on error it keeps on submitting the form
if (typeof geoCountry !== "undefined") {
if (geoCountry.length === 2){
if (geoCountry === 'XK') {geoCountry = 'RS';}
if (geoCountry !== country){
showMessage('Country Mismatch: ' + geoCountry + ' vs ' + country);
console.log('Country Mismatch: ' + geoCountry + ' vs ' + country);
setLoading(false);
return false;
}
}else{
console.log('Error: GeoCountry: ' + geoCountry);
showMessage('Error: GeoCountry: ' + geoCountry);
setLoading(false);
return false;
}
}
on return false; it should stop executing right, but instead, it continues down the code towads stripe.confirmPayment
You'll need to share your full handleSubmit code
chatbot not allowing me to send a long message
i'll try in 2 parts
async function handleSubmit(e) {
e.preventDefault();
setLoading(true);
clientName = document.querySelector("#kpm1-name").value;
street = document.querySelector("#kpm1-street").value;
city = document.querySelector("#kpm1-city").value;
postCode = document.querySelector("#kpm1-postal-code").value;
email[0] = document.querySelector("#kpm1-email1").value;
email[1] = document.querySelector("#kpm1-email2").value;
email[2] = document.querySelector("#kpm1-email3").value;
email[3] = document.querySelector("#kpm1-email4").value;
email[4] = document.querySelector("#kpm1-email5").value;
country = document.querySelector("#kpm1-country").value;
kpmVersion = document.querySelector("#kpmVersion").value;
if (isNaN(kpmVersion)) {
kpmVersion = 5;
}
var xcombo = document.querySelector("#kpm1-country");
var countryName = xcombo.options[xcombo.selectedIndex].text;
var cardButton = document.getElementById('submit');
var clientSecret = cardButton.dataset.secret;
if (clientSecret.length === 0){
showMessage("Unable to authorize intent.");
setLoading(false);
return false;
}
const {error: submitError} = await elements.submit();
if (submitError) {
showMessage("Submit Error!");
return;
}
var geoCountry = "undefined";
$.getJSON("https://ssl.geoplugin.net/json.gp?k=#########", function( data ) {
geoCountry = JSON.parse(JSON.stringify(data.geoplugin_countryCode));
if (typeof geoCountry !== "undefined") {
if (geoCountry.length === 2){
if (geoCountry === 'XK') {geoCountry = 'RS';}
if (geoCountry !== country){
showMessage('Country Mismatch: ' + geoCountry + ' vs ' + country);
setLoading(false);
return false;
}
}else{
showMessage('Error: GeoCountry: ' + geoCountry);
setLoading(false);
return false;
}
}
});
stripe.confirmPayment({
so it should be exiting on country mismatch:
showMessage('Country Mismatch: ' + geoCountry + ' vs ' + country);
setLoading(false);
return false; /// exit sub <<<
It's because you return false statement is inside a function block (getJSON), so it just terminates that function and not the overall handleSubmit
ok let me set a variable and check on function exit
You probably want to wrap all your logic in a try/catch block and throw
shouldn't this fix the issue?
var geoCountry = "undefined";
var errRet = 0;
$.getJSON("https://ssl.geoplugin.net/json.gp?k=########", function( data ) {
geoCountry = JSON.parse(JSON.stringify(data.geoplugin_countryCode));
if (typeof geoCountry !== "undefined") {
if (geoCountry.length === 2){
if (geoCountry === 'XK') {geoCountry = 'RS';}
if (geoCountry !== country){
showMessage('Country Mismatch: ' + geoCountry + ' vs ' + country);
console.log('Country Mismatch: ' + geoCountry + ' vs ' + country);
errRet = 1;
}
}else{
console.log('Error: GeoCountry: ' + geoCountry);
showMessage('Error: GeoCountry: ' + geoCountry);
errRet = 1;
}
}
});
if (errRet === 1 ){
setLoading(false);
return false;
}
i just tried and same thing
I'm sorry it's very hard to follow the diffs in the code you're sharing
i just added a VARIABLE (errRet) TO be set to 1 when there is an error.
errRet = 1;
then below i check if the variable is set to 1 then return false
Your errRet is probably isn't in scope in your getJSON function
The scope of a variable declared with var is its current execution context and closures thereof, which is either the enclosing function and functions declared within it, or, for variables declared outside any function, global. Duplicate variable declarations using var will not trigger an error, even in strict mode, and the variable will not lose its value, unless another assignment is performed.
yh it's outside the getJSON
Yes, but inside of getJSON you're just setting a new errRet variable – not re-assigning the value of the other var
aha
but in that case the getJSON is always ISOLATED
even if i return false on error trapping, it would still just be fore the JSON funciton itself
Which is why I recommended a try/catch block and to throw an error rather than just returning
orr i make it in to a separate function and get the result?