0

I am bit confused on why I am getting 'msg' is undefined error (alert(msg) line) in below code. if I have at least one incorrect address ( address: 0) then I would expect below code to set inValidUser =1 and also set msg variable and then break the the loop. However, I then get a javascript error " Error: 'msg' is undefined." Any ideas?

function test(userData) {
    var myArr = [];
    var i;
    for (i = 1; i <= 3; i++) {
        myArr.push(
            jQuery.ajax({
            type: "GET",
            url: "http:/c.html/" + i,
            });
        );
    }

    $.when.apply($, myArr).done(function() {
        var i = 0;
        var invalidUser = 0;
        var tableData = [];

        $.each(arguments, function (idx, args) {
            if (args[0].address === 0) {
                invalidUser = 1;
                var msg = "User Address " + userData[j].address + " not correct";
                return false;
            } else {
                tableData.push({
                    name: userData[i].firstname,
                    age: userData[i].age
                });
            }
            i++;
        });
        if (invalidUser `enter code here`=== 1) {    
            alert(msg);
        } else {
            addTableData(tableData);
        }
    }).fail (function (jqXHR, textStatus) {
        //oops..failed
    });   
}
2
  • 1
    The msg variable is not in the scope of your alert(). Define it at the top of apply().done(), and remove the var keyword inside the $.each() Commented Jun 25, 2015 at 21:22
  • Because you declared it with a var inside a function. Commented Jun 25, 2015 at 21:22

1 Answer 1

2

You have a scope error in your code. When you declare a variable with var, it will be bound to the closest function that the declaration statement appears in.

In this case, it means this:

    $.each(arguments, function (idx, args) {
    //                ^^^^^^^^             ^ this scope
        if (args[0].address === 0) {
            invalidUser = 1;
            var msg = "User Address " + userData[j].address + " not correct";
    //      ^^^ declares variable in new scope
            return false;
        } else {
            tableData.push({
                name: userData[i].firstname,
                age: userData[i].age
            });
        }
        i++;
    });

What you will want to do is make sure that msg is declared in a scope that both uses of msg have access to. That would be:

$.when.apply($, myArr).done(function() {
    var msg;
//  ^^^^^^^^
    var i = 0;
    var invalidUser = 0;
    var tableData = [];

here, in your case.

When you set msg, then, you would use a variable assignment expression, rather than a declaration:

    msg = "User Address " + userData[j].address + " not correct";
Sign up to request clarification or add additional context in comments.

2 Comments

You also have to drop the var before the assignment.
Right. Thought it was implied by the above statement, I'll clarify

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.