0

I am using a callback method to wait for an ajax request to complete that checks if a different person has locked an item.

The callback method simply waits for the ajax request to complete, then pushes the resultant message into the html.

Here is the ajax function:

        function checkLockAndPushModalMessage(url, evaluationId, userIsObserver, callback) {
            console.log('we are in checkandpush');
            url = url + evaluationId + "/"
            $.ajax({
                url: url,
                type: 'GET',
                beforeSend: function (xhr, settings) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                },
                success: function (data) {
                    if (userIsObserver){
                        console.log('user is observer (in success in check & push)')
                         if (data.employee_locked === true) {
                             callback(true);
                        } else {
                             callback(false);
                        }
                    }
                    if (!userIsObserver) {
                        console.log('user is non-observer (in success in check & push)')
                        if (data.admin_locked === true) {
                             callback(true);
                        } else {
                             callback(false);
                        }
                    }
                },
                error: function(jqxhr, textStatus, errorThrown) {
                    // TODO: Handle errors
                    console.log('There was an issue determining if the other use has locked the evaluation!');
                },
            })
        };

The callback method is this:

        function messageToModal(userHasLocked) {
            let isLocked = userHasLocked;
            console.log('in messageToModal');
            console.log('other user has locked is: ' + userHasLocked);
            console.log('isLocked is: ' + isLocked);
            console.log(typeof userHasLocked);
            if ('foo' != 'bar') {
                // This evaluates fine and logs in console
                console.log('Foo is NOT Bar!')
            }
            let modal_message = "NO NO NO";
            if (isLocked === true) {
                // The other user has locked the evaluation
                let modal_message = "The other user has locked this evaluation.";
            }
            if (isLocked != true) {
                // The other user has not locked the evaluation
                let modal_message = "The other user has not locked this evaluation.";
            }
            // Push the message letting them the know the other users locked status to the modal
            console.log('gotta push the message now: '+ modal_message);
            $("#lockControlMessage").text( modal_message );
        }

And I call it like this:

checkLockAndPushModalMessage(evaluationUrl, evaluationData.id, userIsObserver, messageToModal);

There is a lot of 'dummy data' in there just so I can see what is happening and where we're at in the console.

The other if statement I have in there works as you would expect.

The console looks like this:

console_image

Hanny
  • 580
  • 3
  • 16
  • 44
  • What do you *expect* to happen and what happens instead? – VLAZ Aug 19 '21 at 15:35
  • 1
    Remove `let` in the `if` statement, or change it to `var`. I'd suggest the former, though. You're redefining the variable within each side of the condition – Rory McCrossan Aug 19 '21 at 15:36
  • Sorry, I figured the title was an explanation enough: That the if statement inside the callback method is not executing appropriately. – Hanny Aug 19 '21 at 15:39
  • It's executing absolutely fine, you've misunderstood variable scoping in javascript `if` statements - ive added a useful dupe to the close for you. – Jamiec Aug 19 '21 at 15:40
  • @Hanny "it's not executing properly" or "doesn't work" and similar are ***not*** a good explanation. Because you're only presenting code that you believe does not work. That doesn't mean that we know what a working code is nor what you want a working code to do. Hence why it's important to say what you *expect* and what the discrepancy is.. – VLAZ Aug 19 '21 at 15:47

0 Answers0