#AlienSoft

1 messages · Page 1 of 1 (latest)

fleet patioBOT
limber aspen
#

Can you provide some more context?

proven egret
#

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

limber aspen
#

You'll need to share your full handleSubmit code

proven egret
#

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 <<<

limber aspen
#

It's because you return false statement is inside a function block (getJSON), so it just terminates that function and not the overall handleSubmit

proven egret
#

ok let me set a variable and check on function exit

limber aspen
#

You probably want to wrap all your logic in a try/catch block and throw

proven egret
#

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

fleet patioBOT
limber aspen
#

I'm sorry it's very hard to follow the diffs in the code you're sharing

proven egret
#

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

limber aspen
#

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.

proven egret
#

yh it's outside the getJSON

limber aspen
#

Yes, but inside of getJSON you're just setting a new errRet variable – not re-assigning the value of the other var

proven egret
#

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

limber aspen
#

Which is why I recommended a try/catch block and to throw an error rather than just returning

proven egret
#

orr i make it in to a separate function and get the result?

glass scroll
#

Hi! I'm taking over this thread.

#

Can you try to summarize your question?

proven egret
#

Hi
I'm trying to run a getJSON code and if an error is returned to halt the submission process.

#

but since JSON is isolated, my return false, only exits the JSON code itself rather the the outer submission scope