PDA

View Full Version : Model.load: Cannot call method 'hasId' of undefined



manilodisan
16 Nov 2013, 4:02 AM
I have an application where my model data has the idProperty set to '_id' as per server mongodb data. If I try to load a model instance by given id I get this strange error: Cannot call method 'hasId' of undefined


My model is defined as follows:



Ext.define 'heroico.model.Visitor',
extend: 'Ext.data.Model'
fields: [...]
idProperty: '_id'
proxy:
type: 'memory'


And I'm just hitting
heroico.model.Visitor.load('FcFaNkhH9TJzQ57D5') in my chrome console where `FcFaNkhH9TJzQ57D5` is an actual id present in the store. Any ideas?

Extjs version 4.2.1

uyilmaz
16 Nov 2013, 4:09 AM
I think heroico.model.Visitor is a package name, not an actual object instance, so it is undefined.

manilodisan
16 Nov 2013, 4:15 AM
I think heroico.model.Visitor is a package name, not an actual object instance, so it is undefined.

I just used this as an example: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Model-static-method-load

manilodisan
16 Nov 2013, 4:18 AM
It's the same as in the docs: docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Model-static-method-load

tobiu
16 Nov 2013, 5:56 AM
from what i see from your code, you are defining a class and then you call a method on something with the same name. make sure this is an instance of the class (using Ext.create()).

regarding your namespace i recommend to follow our best practices starting with an uppercase:


Heroico.model.Visitor

manilodisan
16 Nov 2013, 6:44 AM
Ok, I've changed my namespace to capital H, as expected that didn't helped but it's nice to follow best practices. Regarding the docs I just linked here's the code (exactly as in your docs):



Ext.define('MyApp.User', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'}
]
});


MyApp.User.load(10, {
scope: this,
failure: function(record, operation) {
//do something if the load failed
//record is null
},
success: function(record, operation) {
//do something if the load succeeded
},
callback: function(record, operation, success) {
//do something whether the load succeeded or failed
//if operation is unsuccessful, record is null
}
});


We see an ext define on MyApp.User then
MyApp.User.load(10... that's why I figured I'd use the same approach.

I even tried loading it from my controller via
this.getVisitorModel().load(visitor_id) with the same error:
Uncaught TypeError: Cannot call method 'hasId' of undefined

My stores use a memory proxy because my data comes in real time via websockets so I only need a memory proxy. Could that be the case?

manilodisan
16 Nov 2013, 6:56 AM
If I move the proxy config from the model to the store (just because I see various examples) I get a different error: Uncaught TypeError: Cannot call method 'indexOf' of undefined

scottmartin
16 Nov 2013, 8:26 AM
I do see another report of this that mentions this as a possible bug:
http://www.sencha.com/forum/showthread.php?272593

Are you using associations? I see no mention.

Please use our fiddle and provide a working test case:
https://fiddle.sencha.com/#home

btw, there was no need to move your proxy.

tobiu
16 Nov 2013, 9:07 AM
still, you are calling load on the class and not on an instance (s.o.).
since load is not static, this simply can not work.



Ext.define('MyApp.User', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'}
]
});

var myUser = Ext.create('MyApp.User', {
id: 10,
name: 'John Doe'
});

myUser.load(10, {
scope: this,
failure: function(record, operation) {
//do something if the load failed
//record is null
},
success: function(record, operation) {
//do something if the load succeeded
},
callback: function(record, operation, success) {
//do something whether the load succeeded or failed
//if operation is unsuccessful, record is null
}
});


I do agree that the docs are misleading if not wrong at this point.
load() is flagged as static, but if you look at the code, it is not inside the "statics" object which start right below the method (id() is static).
http://docs.sencha.com/extjs/4.2.2/source/Model.html#Ext-data-Model-static-method-load