schumacherj
schumacherj

Reputation: 1324

I am getting an undefined error in my JS, but not sure why

I am getting undefined in my console.log(), at console.log(item.ResponseId); This in turn is causing my $ajax.done() function to fail because the value needs to be the returned value of the initial call. Also, console.log(msg.ResponseId); is outputting an integer as expected.

var rdetail = new Array();
        $('.choiceinput').each(function (i, obj) {
            rdetail.push({
                "ResponseId": 0,
                "ResponseDetailVal": $(this).val(),
                "QuestioChoicesId": $(this).attr('id')
            });
        })


        $.ajax({
            type: "POST",
            url: "/Forecaster/userResponse/",
            data: data
        }).done(function (msg) {
            console.log(msg.ResponseId);
            for (var item in rdetail) {
                item.ResponseId = msg.ResponseId;
                console.log(item.ResponseId);
            }
            console.log("Last: ");
            console.log(rdetail);
            $.post('/Forecaster/userResponseDetails/',
                     JSON.stringify(rdetail),
                null, 'application/json');
        })

Upvotes: 0

Views: 128

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324600

When you write for( item in rdetail), the keys of rdetail are assigned to item one by one. Since a key is a string, assigning a property to it does nothing*.

Did you maybe mean rdetail[item].ResponseId?

* It actually wraps the primitive string in a String() object, assigns the property to it, then discards it

Upvotes: 5

Related Questions