环回:如何在 afterRemote 中访问关系
Loopback: How to access relationship in an afterRemote
我为仓库模型设置了以下 afterRemote 方法:
Depot.afterRemote('find', function(context, depots, next) {
context.result.forEach(depot => {
console.log(depot.drivers);
depot.address = depot.street_name + ' ' + depot.door_number;
depot.driver_count = depot.drivers.length;
});
next()
})
在 json 响应中 depot.drivers 包含正确的数据。
然而,当我尝试在上述方法中访问 depot.drivers 时,我得到了一些奇怪的输出:
{ [Function: f]
_receiver: { postal_code: '1216BS',
door_number: '3',
street_name: 'Straat naam',
city: 'Hilversum',
lat: null,
lng: null,
created: 2018-04-03T01:49:12.000Z,
id: 23,
distributor_id: 2,
distributor: { sys_id: 1,
name: 'distributeur naam',
contact_person: 'Persoon B',
phone: '000-000000',
email: 'info@d.nl',
created: 2018-03-06T00:22:33.000Z,
id: 2 },
drivers: List [] },
_scope: { where: { hub_id: 23 } },
_targetClass: 'driver',
find: [Function],
getAsync: [Function],
build: [Function: bound method],
create: [Function: bound method],
updateAll: [Function: updateAll],
destroyAll: [Function: destroyAll],
findById: [Function: bound method],
findOne: [Function: findOne],
count: [Function: count],
destroy: [Function: bound method],
updateById: [Function: bound method],
exists: [Function: bound method] }
我最终想添加一个 depot.drivers_count 属性,这样我就可以在前端显示连接到特定站点的驱动程序数量 table。
有人知道怎么做吗?
看起来数据没有转换为 JSON。
我会试试这个:
context.result.forEach(depot => {
depot = depot.toJSON();
console.log(depot.drivers);
depot.address = depot.street_name + ' ' + depot.door_number;
depot.driver_count = depot.drivers.length;
});
在 Loopback 中,可以使用回调函数访问每个模型关系,因为 i/o 是异步的。这样做:
depot.drivers((err, drivers) => {
// handle the error
// do something with the drivers
})
您还可以使用驱动程序模型操作挂钩来跟踪数据更改并更新相关的 Depot.drivers_count
属性:
Driver.observe('after save', function(ctx, next) {
// ctx.instance is the created or updated object
if (ctx.isNewInstance) {
// A new record was saved
// Implementation depends on your models relationships
} else {
// A record was updated
// Implementation depends on your models relationships
}
next()
})
Driver.observe('after delete', (ctx, next) => {
// Implementation depends on your models relationships
next()
})
我为仓库模型设置了以下 afterRemote 方法:
Depot.afterRemote('find', function(context, depots, next) {
context.result.forEach(depot => {
console.log(depot.drivers);
depot.address = depot.street_name + ' ' + depot.door_number;
depot.driver_count = depot.drivers.length;
});
next()
})
在 json 响应中 depot.drivers 包含正确的数据。
然而,当我尝试在上述方法中访问 depot.drivers 时,我得到了一些奇怪的输出:
{ [Function: f]
_receiver: { postal_code: '1216BS',
door_number: '3',
street_name: 'Straat naam',
city: 'Hilversum',
lat: null,
lng: null,
created: 2018-04-03T01:49:12.000Z,
id: 23,
distributor_id: 2,
distributor: { sys_id: 1,
name: 'distributeur naam',
contact_person: 'Persoon B',
phone: '000-000000',
email: 'info@d.nl',
created: 2018-03-06T00:22:33.000Z,
id: 2 },
drivers: List [] },
_scope: { where: { hub_id: 23 } },
_targetClass: 'driver',
find: [Function],
getAsync: [Function],
build: [Function: bound method],
create: [Function: bound method],
updateAll: [Function: updateAll],
destroyAll: [Function: destroyAll],
findById: [Function: bound method],
findOne: [Function: findOne],
count: [Function: count],
destroy: [Function: bound method],
updateById: [Function: bound method],
exists: [Function: bound method] }
我最终想添加一个 depot.drivers_count 属性,这样我就可以在前端显示连接到特定站点的驱动程序数量 table。
有人知道怎么做吗?
看起来数据没有转换为 JSON。
我会试试这个:
context.result.forEach(depot => {
depot = depot.toJSON();
console.log(depot.drivers);
depot.address = depot.street_name + ' ' + depot.door_number;
depot.driver_count = depot.drivers.length;
});
在 Loopback 中,可以使用回调函数访问每个模型关系,因为 i/o 是异步的。这样做:
depot.drivers((err, drivers) => {
// handle the error
// do something with the drivers
})
您还可以使用驱动程序模型操作挂钩来跟踪数据更改并更新相关的 Depot.drivers_count
属性:
Driver.observe('after save', function(ctx, next) {
// ctx.instance is the created or updated object
if (ctx.isNewInstance) {
// A new record was saved
// Implementation depends on your models relationships
} else {
// A record was updated
// Implementation depends on your models relationships
}
next()
})
Driver.observe('after delete', (ctx, next) => {
// Implementation depends on your models relationships
next()
})