8

I am having trouble getting the errorCount property to increase during code execution. The problem I am having is occurring inside of the $.ajax request, more specifically the addError() method.

If I use the following code below to check the current count of errorCount it always returns 0 even though I have manually created an error to occur. But inside of the ajax method after I call addError() and then check the value of errorCount it shows 1 like it should. What did I do wrong?

var boom = new test(settings, formData, search);
console.log(boom.errorCount);
boom.queueCalls(settings);
console.log(boom);
console.log(boom.errorCount);

Here is the object code:

function test(a, b, c) {
    this.settings = a;
    this.formData = b;
    this.search = c;
    this.errorCount = 0;
}

test.prototype = {
    constructor: test,
    queueEmails:function(settings, formData, search) {
        var url = '/example-url-endpoint';
        var data = {postData: settings + "&" + formData + "&" + search};
        this.sendRequest(url, data);
    },
    queueCalls:function(settings) {
        var url = '/example-url-endpoint2';
        this.sendRequest(url, settings);
    },
    addMessage:function(response) {
        flashMessage(response.Message, response.Result);
    },
    addError:function() {
        this.errorCount++;
    },
    sendRequest:function(url, data) {
        var blah = this;

        j$.ajax({
            type: "POST",
            url: url,
            data: data,
            dataType: 'json',
            success: function(data) {
                response = JSON.parse(data);
                if(response.Result != 'error') {
                    blah.addMessage(response);
                } else {
                    blah.addMessage(response);
                    blah.addError();
                    console.log(blah.errorCount);
                }
            },
            error: function(e, textStatus, errorThrown) {
                blah.addError();
                console.log(blah.errorCount);
                alert("There was an error creating the queue");
            }
        });
    }
}
1
  • 1
    This is because the call is asynchronous, it shows 0 before the error returns 1. Commented Feb 25, 2016 at 19:53

1 Answer 1

3

The problem is you are doing an asynchronous (AJAX) call. When you call the queueCalls function, it makes the AJAX call, then runs your console.log statements. It does not wait until the AJAX call is done, and you have received your errors to run the console statements. If you want to do that, look at the jQuery documentation for .ajax(), and either make your AJAX call not asynchronous, or put your console statements in a .done() event handler that will fire after the AJAX call is complete.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.