Available models inside the isomorphic Loopback client

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

The company model uses the MySQL connector and profiles that reside inside the ElasticSearch instance.

The loopback client works fine in my front-end app, the following code works successfully:

let lbclient = window.require('lbclient')

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

Run codeHide result


My goal is to fetch profiles by 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
   }
})
      

Run codeHide result


general / model / 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": {}
}
      

Run codeHide result


general / model / 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": {}
}
      

Run codeHide result


client / loopback / models / remote company.json

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

Run codeHide result


client / loopback / models / remote profile.json

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

Run codeHide result


+3


source to share


1 answer


You need to include profiles in your request like this:



lbclient.models.RemoteCompany.findById(8, {include: 'profiles'}, (err, company) => {

})

      

+1


source







All Articles