0

I'm having trouble passing a variable declared in an $.each() function to Prototype function. I'm receiving the error Uncaught ReferenceError: prices is not defined

Compare.prototype.results = function (answers) {

    $.ajax({
        type: 'POST',
        dataType: 'json',
        data: {
            answers: answers
        },

        success: function (data) {

            $.each(data, function (index, dataItem) {

                var prices = [],
                    priceData = dataItem.pricing_term,
                    priceObj = JSON.parse(priceData);

                $.each(priceObj, function (term, pricing) {
                    prices.push(term, pricing);
                });

            });
            Compare.prototype.show(data, prices);
        }

    });
}

I want to be able to populate the prices variable and pass it to be used with the data that is originally returned from the ajax call. I am new to javascript, if there is a cleaner way to go about writing this please let me know.

1
  • Then declare the variable outside of $.each. Commented Apr 2, 2014 at 22:19

2 Answers 2

1

It's out of scope

Compare.prototype.results = function (answers) {

    $.ajax({
        type: 'POST',
        dataType: 'json',
        data: {
            answers: answers
        },

        success: function (data) {

            var prices = [];


            $.each(data, function (index, dataItem) {
                var priceData = dataItem.pricing_term,
                    priceObj = JSON.parse(priceData);

                $.each(priceObj, function (term, pricing) {
                    prices.push(term, pricing);
                });

            });

            // same scope

            Compare.prototype.show(data, prices);
        }

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

Comments

1

You have declared your prices array within the scope of the first &.each function. This means you can only access the prices array in that function. You need to declare prices outside of the function, like so:

Compare.prototype.results = function (answers) {

$.ajax({
    type: 'POST',
    dataType: 'json',
    data: {
        answers: answers
    },

    success: function (data) {

        var prices = [];

        $.each(data, function (index, dataItem) {

               var priceData = dataItem.pricing_term;
               var priceObj = JSON.parse(priceData);

            $.each(priceObj, function (term, pricing) {
                prices.push(term, pricing);
            });

        });
        Compare.prototype.show(data, prices);
    }

});
}

This way, prices is available in any of the functions that are within the scope of the success function of the AJAX request.

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.