Results 1 to 11 of 11

Thread: TypeError: cannot use 'in' operator to search for "length" in

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    138

    TypeError: cannot use 'in' operator to search for "length" in

    Hi. I've a mainform2.php where I want to display the result in table or div or any html element for the time being and I coded for the click event of the button to do this.
    Here is my code for mainform2.php for button.click event.
    Code:
    $(document).ready(function(){
            function loadData(){
                  //alert("hello");
                    $.ajax({
                        url: "show-cart.php",
                        type: "POST",
                        success: function(data){         
                        //console.log(data);
    
                        $.each(data, function(key, value){
                            $("#cart-table").append('<tr><td>' + value.item_id + '</td></tr>');
                            $("#showme").append(value.item_name);
                        });
                            
                        }        
                    }); 
                }
                $(document).ready(function(){                
                    $("#btnClear").on("click", function(){                                      
                        $("#order-form").trigger("reset");
                        loadData();
                    });        
                });
            });
    and here is my show-cart.php table
    Code:
    <?php
    
    $conn = mysqli_connect("localhost", "root", "", "chargha") or die ("Your Connection Failed");
    
    $sql = "SELECT items.item_id, items.item_name, items.item_unit_price, cart.order_qty, cart.order_price, cart.order_of, cart.order_instructions, cart.token_id 
    from cart LEFT JOIN items ON cart.item_id = items.item_id";
    $query = mysqli_query($conn, $sql);
    $output = mysqli_fetch_all($query, MYSQLI_ASSOC); //It transform the array into associative one. mysqli_asssoc, num, both
    
    
    echo json_encode($output);
    
    ?>
    When I click the button
    Code:
    input type="button"
    so I get the following error in the Debugger Tab of the browser.
    Paused on Exception
    TypeError: cannot use 'in' operator to search for "length" in "[{"item_id":"1",..."
    Though I've not used any "in" operator anywhere. The jquery.js file is also red in the Debugger Tab.

    I've search so many questions on Google but their answers are not relevant to me.

    Guide me please, Thanks

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,418

    Re: TypeError: cannot use 'in' operator to search for "length" in

    I suspect that that in operator gets used under the hood here:
    php Code:
    1. $.each(data, function(key, value){
    2.     $("#cart-table").append('<tr><td>' + value.item_id + '</td></tr>');
    3.     $("#showme").append(value.item_name);
    4. });
    I suspect that the issue is that you're treating the value of data as a list and trying to loop over it when it is actually just a single object, hence there is no length and no list for items to be in.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    138

    Re: TypeError: cannot use 'in' operator to search for "length" in

    Thanks JMC
    Actually there is a join query which is bringing the entire table record and here I've to display all the items so I used
    Code:
    $.each
    Even if I just display the data into the console or as pop up alert, so is the error there.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,468

    Re: TypeError: cannot use 'in' operator to search for "length" in

    As it currently is, it's only going to include what's in the cart table... so, is there anything in that table? If not, then that maybe the issue. What do you get when you hit show-cart.php directly? That should give you a big hint as to what the js is seeing. You'll either get back an empty json string, which is what my guess is, or you'll get back a JSON that's wrapped a bit differently from what you're expecting (I've had that happen to me before).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    138

    Re: TypeError: cannot use 'in' operator to search for "length" in

    @tg Thank you tg for your devotion.

    Yes the cart table has the data and the 2nd block of my code in my question has a join query which is bringing the result. The then becomes a 2 dim associative array and then json_encode transforms the 2 dim array into JSON format as API.
    I've made a console output and its showing the result perfectly.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,468

    Re: TypeError: cannot use 'in' operator to search for "length" in

    aaaaand? What's the output?


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    138

    Re: TypeError: cannot use 'in' operator to search for "length" in

    Yeah, that's a question?
    The problem is that when the data is displayed in the console so it also associated with error:
    My data. Don't consider the NULLs beacuse its a dummy data.
    [{"item_id":"4","item_name":"Chicken Boti","item_unit_price":"150","order_qty":"2","order_price":"300","order_of":"Hasnain, Shah sb","order_instructions":"not","token_id":"2"},{"item_id":"3","item_name":"Malai Boti","item_unit_price":"150","order_qty":"3","order_price":"450","order_of":"Hasnain, Shah sb","order_instructions":"not","token_id":"2"},{"item_id":"1","item_name":"Kabuli Pulao with Chicken","item_unit_price":"80","order_qty":"2","order_price":"160","order_of":"Hasnain, Shah sb","order_instructions":"not","token_id":"2"},{"item_id":"2","item_name":"Kabuli Pulao without Chicken","item_unit_price":"40","order_qty":"3","order_price":"120","order_of":"Hasnain, Shah sb","order_instructions":"not","token_id":"2"},{"item_id":null,"item_name":null,"item_unit_price":nu ll,"order_qty":"0","order_price":"0","order_of":"","order_instructions":"","token_id":"0"},{"item_id ":null,"item_name":null,"item_unit_price":null,"order_qty":"0","order_price":"0","order_of":"","orde r_instructions":"","token_id":"0"},{"item_id":"4","item_name":"Chicken Boti","item_unit_price":"150","order_qty":"3","order_price":"450","order_of":"jamil","order_instruct ions":"not","token_id":"5"},{"item_id":"4","item_name":"Chicken Boti","item_unit_price":"150","order_qty":"10","order_price":"1500","order_of":"test1","order_instru ctions":"no","token_id":"10"}]
    And has the same error, I posted in post #1

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,468

    Re: TypeError: cannot use 'in' operator to search for "length" in

    Yeah, it has the same error because you're still running the same code... I was expecting you to just hit show-cart.php directly, and post the results, but hey... what ever works...
    try this, and see what the results are... the output is going to be in your console thought:
    Code:
    $(document).ready(function(){
            function loadData(){
                  //alert("hello");
                    $.ajax({
                        url: "show-cart.php",
                        type: "POST",
                        success: function(data){         
                        console.log(data);
                        $.each(data, function(key, value){
                            console.log(" --> " + value);
                        });
                            
                        }        
                    }); 
                }
                $(document).ready(function(){                
                    $("#btnClear").on("click", function(){                                      
                        $("#order-form").trigger("reset");
                        loadData();
                    });        
                });
            });
    You should get a line starting with " --> " followed by a value ... hopefully you get more than one line...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    138

    Re: TypeError: cannot use 'in' operator to search for "length" in

    I think I did not understood. I ask your pardon please.

    Here is my show-cart.php result directly executed in browser: I've attached the screenshots.
    Name:  show-cart2.JPG
Views: 52
Size:  68.9 KB

    And After playing the your code: There is a screenshot.

    The bottom shows Console Tab of Inspect Element of Mozilla Firefox Browswer and the pink text on right side is Debugger Tab.
    Name:  main_code_Inspect_Element2.jpg
Views: 58
Size:  47.7 KB

    Thank you.
    Last edited by colrh; May 26th, 2021 at 01:45 PM.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,468

    Re: TypeError: cannot use 'in' operator to search for "length" in

    Hmmm.... kay....
    Let's try this...
    Code:
    $(document).ready(function(){
            function loadData(){
                  //alert("hello");
                    $.ajax({
                        url: "show-cart.php",
                        type: "POST",
                        success: function(data){         
                        console.log(data);
                        //$.each(data, function(key, value){
                        //    console.log(" --> " + value);
                        //});
                        $.each({ name: "John", lang: "JS" }, function( k, v ) {
                          alert( "Key: " + k + ", Value: " + v );
                         });                        
                        }        
                    }); 
                }
                $(document).ready(function(){                
                    $("#btnClear").on("click", function(){                                      
                        $("#order-form").trigger("reset");
                        loadData();
                    });        
                });
            });
    And if that works without error... try this:
    Code:
    $(document).ready(function(){
            function loadData(){
                  //alert("hello");
                    $.ajax({
                        url: "show-cart.php",
                        type: "POST",
                        success: function(data){         
                        console.log(data);
                        //$.each(data, function(key, value){
                        //    console.log(" --> " + value);
                        //});
                        $.each([{ name: "John", lang: "JS" }, { name: "Foo", lang: "Bar" }], function( k, v ) {
                          alert( "Key: " + k + ", Value: " + v );
                         });                        
                        }        
                    }); 
                }
                $(document).ready(function(){                
                    $("#btnClear").on("click", function(){                                      
                        $("#order-form").trigger("reset");
                        loadData();
                    });        
                });
            });
    I suspect the first will work, and the second will fail...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    138

    Re: TypeError: cannot use 'in' operator to search for "length" in

    Yes. The first one work and the alert appeared with values.
    In the second code this [object Object] appeared in the alert.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width