填充并返回给定关联记录的外键记录。
GET /:model/:id/:association
如果指定的关联是复数(“集合”),则此操作将关联记录列表作为 JSON 编码的字典数组(纯 JavaScript 对象)返回。如果指定的关联是单数(“模型”),则此操作将关联记录作为 JSON 编码的字典返回。
| 参数 | 类型 | 详情 |
|---|---|---|
| model | 包含模型的 标识。 例如: 'purchase'(在 GET /purchase/47/cashier 中) |
|
| id | 父记录的主键。 例如: '47'(在 GET /purchase/47/cashier 中) |
|
| association | 关联的名称。 例如: 'cashier'(在 GET /purchase/47/cashier 中)或 'products'(在 GET /purchase/47/products 中) |
|
| where | 您可以选择提供一个 where 参数,其中包含 Waterline 查询条件 的 WHERE 部分(以 JSON 字符串编码),而不是基于特定属性进行过滤。这允许您利用 contains、startsWith 和其他子属性条件修饰符来执行更强大的 find() 查询。例如: ?where={"name":{"contains":"theodore"}} |
|
| limit | 要返回的最大记录数(对分页很有用)。默认为 30。 例如: ?limit=100 |
|
| skip | 要跳过的记录数(对分页很有用)。 例如: ?skip=30 |
|
| sort | 排序顺序。默认情况下,返回的记录按主键值升序排序。 例如: ?sort=lastName%20ASC |
|
| select | 要包含在结果中每个记录中的属性,指定为逗号分隔的列表。默认情况下,选择所有属性。对于复数(“集合”)关联属性无效。 例如: ?select=name,age。 |
|
| omit | 要从结果中每个记录中排除的属性,指定为逗号分隔的列表。不能与 select 结合使用。对于复数(“集合”)关联属性无效。例如: ?omit=favoriteColor,address。 |
填充执行订单 #47 的 cashier。
`GET /purchase/47/cashier`
{
"name": "Dolly",
"id": 7,
"createdAt": 1485462079725,
"updatedAt": 1485476060873,
}
使用 jQuery
$.get('/purchase/47/cashier', function (cashier) {
console.log(cashier);
});
使用 Angular
$http.get('/purchase/47/cashier')
.then(function (cashier) {
console.log(cashier);
});
使用 sails.io.js
io.socket.get('/purchase/47/cashier', function (cashier) {
console.log(cashier);
});
使用 cURL
curl https://:1337/purchase/47/cashier
您还可以填充集合。例如,要填充员工 #7 的 involvedInPurchases
GET /employee/7/involvedInPurchases
[
{
"amount": 10000,
"createdAt": 1485476060873,
"updatedAt": 1485476060873,
"id": 47,
"cashier": 7
},
{
"amount": 50,
"createdAt": 1487015460792,
"updatedAt": 1487015476357,
"id": 52,
"cashier": 7
}
]
- 在上面的第一个示例中,如果订单 #47 没有
cashier(即null),则此操作将返回 404 状态代码。以上示例假设启用了“rest”蓝图路由(或者您已将此蓝图操作绑定为可比较的 自定义路由),并且您的项目至少包含一个空的
Employee模型以及一个Purchase模型,并且Employee具有关联属性:involvedInPurchases: {model: 'Purchase'}并且Purchase具有cashier: {model: 'Employee'}。您可以通过运行以下命令快速实现此目的$ sails new foo $ cd foo $ sails generate model purchase $ sails generate model employee...然后编辑
api/models/Employee.js和api/models/Purchase.js。