2017-08-05 36 views
3

我想使用Loopback同构客户端为给定公司获取配置文件。Loopback同构客户端访问相关模型

公司模型利用MySQL连接器和配置文件存在于ElasticSearch实例中。

环回客户端我的前端应用程序内正常使用,下面的代码成功运行:

let lbclient = window.require('lbclient') 
 

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

我的目标是作为一个嵌套的资源获取模式:

/api/companies/{id}/profiles/

问题是:为什么下面的代码在客户端上不起作用?

// 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 
 
    } 
 
})

通用/模型/ 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": {} 
 
}

通用/模型/ 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": {} 
 
}

客户机/环回/模型/远程company.json

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

客户机/环回/模型/远程简档。 json

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

回答

1

您需要在查询配置文件是这样的:

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

}) 
+0

谢谢你的回答,这是我走过来的第一个解决方案,但不幸的是,我不能以这种方式执行“计数”。 –

+0

@AntonioTrapani'计数'什么? –

+0

统计公司拥有的配置文件并受过滤器限制。典型的用例是分页。 –