Skip to content

Can't app.models['xxxx'] to retrieve when the model is create via app.model('Xxxx') #464

Closed

Description

@clark0x

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

related code is around here: https://github.com/strongloop/loopback/blob/master/lib/application.js#L147

I'm not sure which case should be correct, but it's better to be consistent.

Activity

  1. johnsoftek commented on Aug 12, 2014

    @johnsoftek

    "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:

    I'm try to use loopback-testing
    https://github.com/strongloop/loopback-testing to write tests for my
    loopback 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

    related code is around here:
    https://github.com/strongloop/loopback/blob/master/lib/application.js#L147

    I'm not sure which case should be correct, but it's better to be
    consistent.

    —
    Reply to this email directly or view it on GitHub
    #464.

  2. clark0x commented on Aug 12, 2014

    @clark0x
    ContributorAuthor

    @johnsoftek you miss my point. Use another name is the same:

    app.model('Xxx-yyy', { dataSource: 'db' });
    console.log(!!app.models['XxxYyy']); // true
    console.log(!!app.models['xxxYyy']); // false
    console.log(!!app.models['xxx-yyy']); // false
    console.log(!!app.models['Xxx-yyy']); // true
    

    VS

    app.model('xxx-yyy', { dataSource: 'db' });
    console.log(!!app.models['XxxYyy']); // true
    console.log(!!app.models['xxxYyy']); // true
    console.log(!!app.models['xxx-yyy']); // true
    console.log(!!app.models['Xxx-yyy']); // false
    
  3. clark0x commented on Aug 12, 2014

    @clark0x
    ContributorAuthor

    @johnsoftek

    If you create app.model.user, then "user" now exists but the built-in "User" model still exists.

    I have check what you say. I think you are right and wrong. If you talk about loopback then you are right, if you talk about app (app = loopback()) then you are wrong. app has its own model registery (app._models, check https://github.com/strongloop/loopback/blob/master/lib/application.js#L147)

    var loopback = require('loopback');
    var app = loopback();
    app.dataSource('db', {connector: loopback.Memory});
    
    console.log(!!loopback.findModel('user')); // false
    console.log(!!loopback.findModel('User')); // true
    
    loopback.createModel('user', {}, {});
    
    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
    
    console.log(!!app.models['user']); // true
    console.log(!!app.models['User']); // true
    console.log(app.models['user'] === app.models['User']); // true
    
  4. 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
  5. 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
  6. johnsoftek commented on Aug 13, 2014

    @johnsoftek

    Thanks for the clarification.

    On 13 August 2014 09:31, Clark Wang notifications@github.com wrote:

    @johnsoftek https://github.com/johnsoftek

    If you create app.model.user, then "user" now exists but the built-in
    "User" model still exists.

    I have check what you say. I think you are right and wrong. If you talk
    about loopback then you are right, if you talk about app (app = loopback())
    then you are wrong. app has its own model registery (app._models, check
    https://github.com/strongloop/loopback/blob/master/lib/application.js#L147
    )

    var loopback = require('loopback');
    var app = loopback();
    app.dataSource('db', {connector: loopback.Memory});

    console.log(!!loopback.findModel('user')); // false
    console.log(!!loopback.findModel('User')); // true

    loopback.createModel('user', {}, {});

    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

    console.log(!!app.models['user']); // true
    console.log(!!app.models['User']); // true
    console.log(app.models['user'] === app.models['User']); // true

    —
    Reply to this email directly or view it on GitHub
    #464 (comment).

  7. clark0x commented on Aug 15, 2014

    @clark0x
    ContributorAuthor

    This is not a well considered question or issue. I may ask a better one later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions