Access related models inside the Loopback isomorphic client

Viewed 189

I want to fetch the profiles for a given company using the Loopback isomorphic client.

The Company model makes use of the MySQL connector and the profiles live inside an ElasticSearch instance.

The loopback client is working perfectly inside my front-end app, the following code runs successfully:

let lbclient = window.require('lbclient')

lbclient.models.RemoteProfile.find().done(profiles => {
  // do something with the profiles array
})

My goal is to fetch profiles as a nested resource:

/api/companies/{id}/profiles/

The question is: why the following code doesn't work on the client?

// This is the code I execute on the client
lbclient.models.RemoteCompany.findById(8, (err, company) => {
  company.profiles // undefined
})

// This code works very well on the server
Company.findById(8, (err, company) => {
   company.profiles((err, profiles) => {
     // profiles belong the the company having id=8
   }
})

common/models/company.json

{
  "name": "Company",
  "plural": "companies",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true,
      "default": "My Company"
    }
  },
  "validations": [],
  "relations": {
    "profiles": {
      "type": "hasMany",
      "model": "Profile",
      "foreignKey": "company_id"
    }
  },
  "acls": [],
  "methods": {}
}

common/models/profile.json

{
  "name": "Profile",
  "plural": "profiles",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string"
    }, 
  },
  "validations": [],
  "relations": {
    "company": {
      "type": "belongsTo",
      "model": "Company",
      "foreignKey": "company_id"
    }
  },
  "acls": [],
  "methods": {}
}

client/loopback/models/remote-company.json

{
  "name": "RemoteCompany",
  "base": "Company",
  "plural": "companies",
  "trackChanges": false,
  "enableRemoteReplication": true
}

client/loopback/models/remote-profile.json

{
  "name": "RemoteProfile",
  "base": "Profile",
  "plural": "profiles",
  "trackChanges": false,
  "enableRemoteReplication": true
}

1 Answers
Related