Uncaught TypeError: Cannot read property 'length' of undefined

elksie5000

How do I avoid the following error when trying to take data from an AJAX call and plug it into another function when I click the submit button?

The console.log call in the ajax function shows that the data is grabbed and I'd hoped then stored in json_data.

The aim was then to use this data to alter a string submitted through an HTML form.

The error is then returned at the line in the 'click' function:

console.log(json_data.length);

<title>Test Form</title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<style>
    #results_box {
        border: red 5px solid;
                }

    #place {
        border: #cccccc 1px solid;

    }           
</style>
<script type="text/javascript">
$(document).ready(function() {
    var json_source = "https://spreadsheets.google.com/feeds/list/0ApL1zT2P00q5dG1wOUMzSlNVV3VRV2pwQ2Fnbmt3M0E/od7/public/basic?alt=json";
    var string_data ="";
    var json_data = $.ajax({
        dataType: 'jsonp',
        url: json_source,
        success: function(data){
            var data_obj = [];
            for (i=0; i<data.feed.entry.length; i++){
                var el = {'key': data.feed.entry[i].title['$t'], 'value': '<p><a href="'+data.feed.entry[i].content['$t']+'>'+data.feed.entry[i].title['$t']+'</a></p>'};
                data_obj.push(el)};

            console.log("data grabbed");    

            return data_obj;

        },      

        error: function(jqXHR, textStatus, errorThrown){ 
                        $('#results_box').html('<h2>Something went wrong!</h2><p><b>' + textStatus  + '</b> ' + errorThrown  + '</p>');
        }
    }); 

    $(':submit').click(function(event, json_data){
        event.preventDefault();
        console.log(json_data.length);

        //function
        if ($('#place').val() !=''){
            var copy_string = $('#place').val();
            var converted_string = copy_string;
            for (i=0; i<json_data.length; i++){
                //console_log(data.feed.entry[i].title['$t']);
                converted_string = converted_string.replace(json_data.feed.entry[i].title['$t'], 
                    '<a href="'+json_data.feed.entry[i].content['$t']+'>'+json_data.feed.entry[i].title['$t']+'</a>');
            }  
            $('#results_box').text(converted_string).html();
        }
    });

});//document ready end 

</script>
</head>

<body>
    <div id="wrapper">
    <div id="query_box" class="panel">  
        <form id="form_submit"><h4>Copy to process:</h4>
          <textarea id="place"></textarea>
          <input type="submit" value="Go" />

        </form>
    </div>
   <div id="results_box" >Results will appear here</div>  
</div>

Miquel Las Heras

You are not passing the variable correctly. One fast solution is to make a global variable like this:

var global_json_data;
$(document).ready(function() {
    var json_source = "https://spreadsheets.google.com/feeds/list/0ApL1zT2P00q5dG1wOUMzSlNVV3VRV2pwQ2Fnbmt3M0E/od7/public/basic?alt=json";
    var string_data ="";
    var json_data = $.ajax({
        dataType: 'json', // Return JSON
        url: json_source,
        success: function(data){
            var data_obj = [];
            for (i=0; i<data.feed.entry.length; i++){
                var el = {'key': data.feed.entry[i].title['$t'], 'value': '<p><a href="'+data.feed.entry[i].content['$t']+'>'+data.feed.entry[i].title['$t']+'</a></p>'};
                data_obj.push(el)};

            console.log("data grabbed");  
            global_json_data =   data_obj;

            return data_obj;


        },      

        error: function(jqXHR, textStatus, errorThrown){ 
                        $('#results_box').html('<h2>Something went wrong!</h2><p><b>' + textStatus  + '</b> ' + errorThrown  + '</p>');
        }
    }); 

    $(':submit').click(function(event){
        var json_data = global_json_data;
        event.preventDefault();
        console.log(json_data.length);

        //function
        if ($('#place').val() !=''){
            var copy_string = $('#place').val();
            var converted_string = copy_string;
            for (i=0; i<json_data.length; i++){
                //console_log(data.feed.entry[i].title['$t']);
                converted_string = converted_string.replace(json_data.feed.entry[i].title['$t'], 
                    '<a href="'+json_data.feed.entry[i].content['$t']+'>'+json_data.feed.entry[i].title['$t']+'</a>');
            }  
            $('#results_box').text(converted_string).html();
        }
    });

});//document ready end 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined(…)

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined when using setInterval

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete

From Dev

DataTables - Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

Getting Uncaught TypeError: Cannot read property 'length' of undefined on leafletjs

From Dev

clipboardData - Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined when there is a parent

From Dev

Javascript: Simple Function - Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

"Uncaught TypeError: Cannot read property 'length' of undefined" after defining var

From Dev

jquery error uncaught typeerror: cannot read property 'length' of undefined

From Dev

Ajax Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

Uncaught TypeError: Cannot read property 'length' of null

From Dev

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

From Dev

Uncaught TypeError: Cannot read property 'substr' of undefined

From Dev

Uncaught TypeError: Cannot read property 'createRouteFromReactElement' of undefined

From Dev

Uncaught TypeError: Cannot read property 'ajax' of undefined

From Dev

Uncaught TypeError: Cannot read property 'call' of undefined

From Dev

Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined

From Dev

Uncaught TypeError: Cannot read property 'addMethod' of undefined

From Dev

Uncaught TypeError: Cannot read property 'draw' of undefined

From Java

Uncaught TypeError: Cannot read property 'top' of undefined

From Dev

Uncaught TypeError: Cannot read property 'parentNode' of undefined

From Java

Uncaught TypeError: Cannot read property 'linear' of undefined

From Dev

Uncaught TypeError: Cannot read property 'document' of undefined

From Dev

Uncaught TypeError: Cannot read property 'getZoom' of undefined

From Dev

Uncaught TypeError: Cannot read property 'match' of undefined

From Dev

Uncaught TypeError: Cannot read property 'arrayToDataTable' of undefined

From Dev

Uncaught TypeError: Cannot read property 'createRecord' of undefined

From Dev

Uncaught TypeError: Cannot read property 'login' of undefined

Related Related

  1. 1

    Uncaught TypeError: Cannot read property 'length' of undefined(…)

  2. 2

    Uncaught TypeError: Cannot read property 'length' of undefined when using setInterval

  3. 3

    Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete

  4. 4

    DataTables - Uncaught TypeError: Cannot read property 'length' of undefined

  5. 5

    Getting Uncaught TypeError: Cannot read property 'length' of undefined on leafletjs

  6. 6

    clipboardData - Uncaught TypeError: Cannot read property 'length' of undefined

  7. 7

    Uncaught TypeError: Cannot read property 'length' of undefined when there is a parent

  8. 8

    Javascript: Simple Function - Uncaught TypeError: Cannot read property 'length' of undefined

  9. 9

    "Uncaught TypeError: Cannot read property 'length' of undefined" after defining var

  10. 10

    jquery error uncaught typeerror: cannot read property 'length' of undefined

  11. 11

    Ajax Uncaught TypeError: Cannot read property 'length' of undefined

  12. 12

    Uncaught TypeError: Cannot read property 'length' of null

  13. 13

    Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

  14. 14

    Uncaught TypeError: Cannot read property 'substr' of undefined

  15. 15

    Uncaught TypeError: Cannot read property 'createRouteFromReactElement' of undefined

  16. 16

    Uncaught TypeError: Cannot read property 'ajax' of undefined

  17. 17

    Uncaught TypeError: Cannot read property 'call' of undefined

  18. 18

    Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined

  19. 19

    Uncaught TypeError: Cannot read property 'addMethod' of undefined

  20. 20

    Uncaught TypeError: Cannot read property 'draw' of undefined

  21. 21

    Uncaught TypeError: Cannot read property 'top' of undefined

  22. 22

    Uncaught TypeError: Cannot read property 'parentNode' of undefined

  23. 23

    Uncaught TypeError: Cannot read property 'linear' of undefined

  24. 24

    Uncaught TypeError: Cannot read property 'document' of undefined

  25. 25

    Uncaught TypeError: Cannot read property 'getZoom' of undefined

  26. 26

    Uncaught TypeError: Cannot read property 'match' of undefined

  27. 27

    Uncaught TypeError: Cannot read property 'arrayToDataTable' of undefined

  28. 28

    Uncaught TypeError: Cannot read property 'createRecord' of undefined

  29. 29

    Uncaught TypeError: Cannot read property 'login' of undefined

HotTag

Archive