You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't app.models['xxxx'] to retrieve when the model is create via app.model('Xxxx') #464
I'm try to use loopback-testing to write tests for an untouched loopback example project, but it keep saying that:
cannot get model of name user from app.models
and
cannot get model of name accessToken from app.models
So I study the code, and find out these fact: When retrieve models via app.models, the case of the first letter of the modelName param is important. Whether the retrieve is success depends on the case of the first letter of modelName which used to create this model.
var loopback = require('loopback');
var app = loopback();
app.dataSource('db', {connector: loopback.Memory});
Run following code case by case:
// case 1: if model is created with non-capitalize name, the case of the first letter is irrelevant.
app.model('user', { dataSource: 'db' });
console.log(!!app.models['user']); // true
console.log(!!app.models['User']); // true
// case 2: if model is created with capitalize name, the case of the first letter is important.
app.model('User', { dataSource: 'db' });
console.log(!!app.models['User']); // true
console.log(!!app.models['user']); // false
// case 3, when using exist model like `loopback.User`, same as case 2. This should be the case that built-in models involve.
app.model(loopback.User, { dataSource: 'db' });
console.log(!!app.models['User']); // true
console.log(!!app.models['user']); // false
cannot get model of name accessToken from app.models
So I study the code, and find out these fact: When retrieve models via
app.models, the case of the first letter of the modelName param is
important. Whether the retrieve is success depends on the case of the first
letter of modelName which used to create this model.
var loopback = require('loopback');
var app = loopback();
app.dataSource('db', {connector: loopback.Memory});
Run following code case by case:
// case 1: if model is created with non-capitalize name, the case of the first letter is irrelevant.
app.model('user', { dataSource: 'db' });
console.log(!!app.models['user']); // true
console.log(!!app.models['User']); // true
// case 2: if model is created with capitalize name, the case of the first letter is important.
app.model('User', { dataSource: 'db' });
console.log(!!app.models['User']); // true
console.log(!!app.models['user']); // false
// case 3, when using exist model like loopback.User, same as case 2. This should be the case that built-in models involve.
app.model(loopback.User, { dataSource: 'db' });
console.log(!!app.models['User']); // true
console.log(!!app.models['user']); // false
changed the title [-]Can't `app.models['user']` when User model is create via `app.model('User')`[/-][+]Can't `app.models['xxxx']` when User model is create via `app.model('Xxxx')`[/+]on Aug 12, 2014
changed the title [-]Can't `app.models['xxxx']` when User model is create via `app.model('Xxxx')`[/-][+]Can't `app.models['xxxx']` to retrieve when the model is create via `app.model('Xxxx')`[/+]on Aug 12, 2014
console.log(!!loopback.findModel('user')); // true
console.log(!!loopback.findModel('User')); // true
console.log(loopback.findModel('user') === loopback.findModel('User')); // false, User model still exists
console.log(app.models().length); // 0
app.model(loopback.findModel('User'));
app.model(loopback.findModel('user')); // this will replace User model register above in app
Activity
johnsoftek commentedon Aug 12, 2014
"User" is a built-in model and always exists.
If you create app.model.user, then "user" now exists but the built-in
"User" model still exists.
On 13 August 2014 02:03, Clark Wang notifications@github.com wrote:
clark0x commentedon Aug 12, 2014
@johnsoftek you miss my point. Use another name is the same:
VS
clark0x commentedon Aug 12, 2014
@johnsoftek
I have check what you say. I think you are right and wrong. If you talk about
loopbackthen you are right, if you talk aboutapp(app = loopback()) then you are wrong.apphas its own model registery (app._models, check https://github.com/strongloop/loopback/blob/master/lib/application.js#L147)[-]Can't `app.models['user']` when User model is create via `app.model('User')`[/-][+]Can't `app.models['xxxx']` when User model is create via `app.model('Xxxx')`[/+][-]Can't `app.models['xxxx']` when User model is create via `app.model('Xxxx')`[/-][+]Can't `app.models['xxxx']` to retrieve when the model is create via `app.model('Xxxx')`[/+]johnsoftek commentedon Aug 13, 2014
Thanks for the clarification.
On 13 August 2014 09:31, Clark Wang notifications@github.com wrote:
clark0x commentedon Aug 15, 2014
This is not a well considered question or issue. I may ask a better one later.