0

When I try to load remotely data to grid I get this error for subfields:

Cannot read property 'id' of null

My DataModel:

    Ext.define('ruleDataModel', {
        extend: 'Ext.data.Model',
        fields: [
           { name: 'id'},
           { name: 'createTime', type:'date', dateFormat: 'timestamp', convert:function(v,j){ return (v != null?new Date(v):null);}},
           { name: 'discountPercent'},
           { name: 'discountAmount'},
           { name: 'discountOverSalePriceFlag', type: 'boolean'},
           { name: 'minSalePriceTotal'},
           { name: 'maxCount'},
           { name: 'execOrder'},
           { name: 'clearanceIncludedFlag', type: 'boolean'},
           { name: 'relatedProductMinCount'},
           { name: 'promocodeRuleTypeName', mapping: 'promocodeRuleType.friendlyType'},
           { name: 'groupName'},
           { name: 'productTypeId', mapping: 'productType.id', defaultValue: ''},
           { name: 'productTypeName', mapping: 'productType.name', defaultValue: ''},
           { name: 'relatedProductTypeId', mapping: 'relatedProductType.id', defaultValue: ''},
           { name: 'relatedProductTypeName', mapping: 'relatedProductType.name', defaultValue: ''}
        ], 
        idProperty: 'id'
    });

returned JSON data:

{totalCount: 1, root: [{"productType": {"name":
...
"relatedProductType":null,
...
"execOrder":0,"id":11}]}
efirat
  • 3,679
  • 2
  • 39
  • 43

2 Answers2

0

Check following things :

  1. Check namespace of your model. Is it only ruleDataModel or is it something like foo.bar.ruleDataModel?

  2. There might be some problem with your json. There is a simple way to test if json is correct or not. Save that json in .json file and open that file in your browser. If browser is able to open that json file correctly then your json is correct, otherwise not.

Shekhar
  • 11,438
  • 36
  • 130
  • 186
  • 1. it is only ruleDataModel, it runs perfectly when "relatedProductType" is NOT null... 2. Json is absolutely correct, I tried it with a JSON viewer and it shows correctly. 3. In general, "mapping" usage prevents Extjs to give error. however, in this situation I get same error always... – efirat Apr 01 '13 at 09:32
0

For subfields, DataModel should be defined such that:

Ext.define('ruleDataModel', {
            extend: 'Ext.data.Model',
            fields: [
               { name: 'id'},
               { name: 'createTime', type:'date', dateFormat: 'timestamp', convert:function(v,j){ return (v != null?new Date(v):null);}},
               { name: 'discountPercent'},
               { name: 'discountAmount'},
               { name: 'discountOverSalePriceFlag', type: 'boolean'},
               { name: 'minSalePriceTotal'},
               { name: 'maxCount'},
               { name: 'execOrder'},
               { name: 'clearanceIncludedFlag', type: 'boolean'},
               { name: 'relatedProductMinCount'},
               { name: 'promocodeRuleTypeName', mapping: 'promocodeRuleType.friendlyType'},
               { name: 'groupName'},
               { name: 'productType.id', mapping: 'productType', convert:function(v,j) { console.log(v,j); return (v != null?v.id:"");}},
               { name: 'productType.name', mapping: 'productType', convert:function(v,j) { console.log(v,j); return (v != null?v.name:"");}},
               { name: 'relatedProductType.id', mapping: 'relatedProductType', convert:function(v,j) { console.log(v,j); return (v != null?v.id:"");}},
               { name: 'relatedProductType.name', mapping: 'relatedProductType', convert:function(v,j){ console.log(v,j); return (v != null?v.name:"");}}
            ], 
            idProperty: 'id'
        });

the difference is

Right One:

{ name: 'productType.id', mapping: 'productType', convert:function(v,j) { console.log(v,j); return (v != null?v.id:"");}},

Wrong One:

{ name: 'productTypeId', mapping: 'productType.id', defaultValue: ''}
efirat
  • 3,679
  • 2
  • 39
  • 43