Results 1 to 6 of 6

Thread: Cannot read property 'length' of undefined

  1. #1
    Sencha User
    Join Date
    Aug 2011
    Posts
    31

    Default Cannot read property 'length' of undefined

    Hi guys,

    I have a json file with the data i want to put in my list but apparently it doesn't load any data. When i try to run the program it keeps loading and loading and it gives me the following error:"Cannot read property 'length' of undefined". I think it refers to the records.length so it means basically it's not loading anything into the store.
    This is my Json file:

    Code:
    {'cars': [{        "id": 1,
        },
        {
            "id": 2,
        },
        {
            "id": 3,
        },
        {
            "id": 4,
        },
        {
            "id": 5,
        }],
        
    'brands': [
        {
            "id": 1,
            "car_id": 1,
            "brand": "Mercedes"
        },
        {
            "id": 2,
            "car_id": 2,
            "brand": "Ferrari"
        },
        {
            "id": 3,
            "car_id": 1,
            "brand": "Lotus"
        },
        {
            "id": 4,
            "car_id": 3,
            "brand": "Volvo"
        },
        {
            "id": 5,
            "car_id": 4,
            "brand": "Opel"
        },
        {
            "id": 6,
            "car_id": 4,
            "brand": "WW"
        }]
    }
    And this is my store, list and model:

    Code:
    Auto.models.Brand= Ext.regModel('Brand', {
        idProperty:'id',
        fields: [
            {name: 'id', type:'int'},
            {name: 'car_id', type:'int'},
            {name: 'brand', type:'string'}
        ],
        belongsTo: 'Car',
        proxy: {
            type: 'ajax',
            url: '../data.json',
            reader: {
                type: 'json',
                root: 'brands'
            }
        }
    });    
        
    Auto.models.Car= Ext.regModel('Car', {
        idProperty: 'id',
        fields: [
            {name: 'id', type:'int'},
        ],
        associations:[
            {type:'hasMany', model:'Brand', name:'brands'}
        ],
        proxy: {
            type: 'ajax',
            url: '../data.json',
            reader: {
                type: 'json',
                root: 'cars'
            }
        },    
    });
    
    Auto.stores.Car= new Ext.data.Store({    model: "Car",
        autoLoad: true
    });
    
    
    Auto.stores.Brand= new Ext.data.Store({
        model: "Brand",
        autoLoad: true
    });
    Auto.views.CarList = new Ext.List({
        store: TPL.stores.Car,
        itemTpl: '<span id="{id}">{id}</span>'
    });
    Any idea how about what could be wrong?

  2. #2
    Sencha Developer
    Join Date
    Sep 2008
    Location
    Antioch, IL
    Posts
    1,516

    Default

    For starters, your JSON is horribly incorrect. http://jsonlint.com/

    Get rid of all of those trailing commas and see what happens!

  3. #3
    Sencha User
    Join Date
    Aug 2011
    Posts
    31

    Default

    thanks for the tip, i just corrected it on the link you gave me but the error remains the same
    here's the new Json

    Code:
    {    "cars": [
            {
                "id": 1
            },
            {
                "id": 2
            },
            {
                "id": 3
            },
            {
                "id": 4
            },
            {
                "id": 5
            },
            {
                "id": 6
            }
        ],
        "brands": [
            {
                "id": 1,
                "car_id": 1,
                "brand": "Mercedes"
            },
            {
                "id": 2,
                "car_id": 2,
                "brand": "Ferrari"
            },
            {
                "id": 3,
                "car_id": 3,
                "brand": "Lotus"
            },
            {
                "id": 4,
                "car_id": 4,
                "brand": "Volvo"
            },
            {
                "id": 5,
                "car_id": 5,
                "brand": "Opel"
            },
            {
                "id": 6,
                "car_id": 6,
                "brand": "WW"
            }
        ]
    }

  4. #4
    Sencha Developer
    Join Date
    Sep 2008
    Location
    Antioch, IL
    Posts
    1,516

    Default

    Trailing commas may be to blame, still.

    Run your JS file through JSLint (jslint.com)

    Just looking at your code, I see a trailing comma in your "fields" definition on your "Auto.models.Car" class.

  5. #5
    Sencha User
    Join Date
    Aug 2011
    Posts
    31

    Default

    thanks again, i fixed the trailing commas and also tried to change my stores to JsonStore. So now it gives me a different error: "Uncaught Error: You are using a ServerProxy but have not supplied it with a url." I'm deploying my json file using glassfish and trying to acess through the url. The url does exist, so i don't know what's the problem.


    Edit: I got the problem solved, all it needed was to deploy it on a server

  6. #6
    Sencha Developer
    Join Date
    Sep 2008
    Location
    Antioch, IL
    Posts
    1,516

    Default

    Glad you got things fixed.

Posting Permissions

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