Simple JSQuery truth statement

I have a JSQuerry like this:

UserLookup.trigger({
  onSuccess: function(data) {
    TeamLookup.trigger({
      onSuccess: function (data) {
        if (TeamLookup.data[0].code == "management") {
          return true;
        } else {
          Promise.reject("fail");
        }
      }
    })
 }});

And just for reference, TeamLookup.data[0].code equals 'inactive'

But whether code = 'inactive' or 'management', I am getting neither a true response or a fail.
What am I doing wrong? This seems like something simple I am missing. Basically trying to get this to have a "success" event and a different "failure" event depending on the query result.
Thanks!

try if (TeamLookup.data[0].code === "management")

it's a JS script thing

or the problem is TeamLookup.data[0].code. the onSuccess handler is passed data which you don't use, but the trigger is for UserLookup. so maybe you either meant to use data[0].code or maybe UserLookup.data[0].code?

you can also use console.log("fail") or something to help find lines it fails on or more details (try/catch log error) because it might actually be the Promise.reject that's causing the problem.

in a nut shell, if you're 100% sure it isn't entering the if/else and it never executes anything after it (try adding a console.log() after the if/else) then something in the if or else is causing a crash. in this case there are only a few things that are possibilities here:

  • == is trying to coerce .code into a string
    • it can't coerce so it errors
    • it can coerce, but they aren't equal so it goes to the 'else'
  • coerce doesn't error and they are not equal OR no coercion needed but they still aren't equal
    • then Promise.reject fails.

if you change == to === then we rule out the 1st point and we're down to the problem being with Promise.reject

UserLookup.trigger({
  onSuccess: function(data) {
    TeamLookup.trigger({
      onSuccess: function (data) {
        console.log("Start onSuccess");
        console.log("Data", data);
        console.log("TeamLookup.data[0]", TeamLookup.data[0]);
        let code = TeamLookup.data[0].code;
        try{
        if (code  === "management") {
          console.log("Found 'management'");
          console.log("End onSuccess");
          return true;
        } else {
          console.log("Failing Promise, Found: " + code);
          Promise.reject("fail");
        }
        } catch(err){ console.log("Try/Catch Error", err);
        console.log("End onSuccess");
      }
    })
 }});

this is overkill and not really the best way, but it should give you enough logs to find what's happening. My guess is that TeamLookup.data or TeamLookup.data[0] are actually undefined/null, but you should know after going through the logs as it could be Promise.reject that's failing. if not, let us know if you discovered anything new.

1 Like

I have simplified the code down to something like this:

try {
  var code = 'FI';
  if (code == 'FC') {
    console.log('pass');
    return true;
  } else {
    console.log('fail');
    Promise.reject('fail');
  }
} catch(err) {
  console.log("Try/Catch Error", err);
}

The console shows Ran Successfully and also prints fail.
I have two different event handlers, one for Success and one for Failure.
The event that is set for Failure (a popup dialog saying failure) never fires.
Whether the if statement is == or ===, it has the same result.

I'm stupid.
it should be:

 } else {
    console.log('fail');
    return Promise.reject('fail');
  }