PDA

View Full Version : Cannot read property 'length' of undefined



1chiban
29 Aug 2011, 8:49 AM
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:


{'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:




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?:s

arthurakay
29 Aug 2011, 12:14 PM
For starters, your JSON is horribly incorrect. http://jsonlint.com/

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

1chiban
30 Aug 2011, 12:51 AM
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


{ "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"
}
]
}

arthurakay
30 Aug 2011, 9:12 AM
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.

1chiban
31 Aug 2011, 12:46 AM
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

arthurakay
31 Aug 2011, 8:15 AM
Glad you got things fixed.