xfscrypt
xfscrypt

Reputation: 276

Loopback: How to access relationship in an afterRemote

I have the following afterRemote method setup for the depot model:

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

In the json response depot.drivers contains proper data.

However when I try to access depot.drivers in the above method I get some weird output:

{ [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: '[email protected]',
        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] }

I would eventually like to add a depot.drivers_count property so I can display the amount of drivers connected to a specific depot in a front-end table.

Anyone has an idea how to do this?

Upvotes: 0

Views: 231

Answers (2)

Antonio Trapani
Antonio Trapani

Reputation: 811

In Loopback every model relationship can be accessed using a callback function because i/o is async. Do like this:

depot.drivers((err, drivers) => {
  // handle the error
  // do something with the drivers
})

You can also use the Driver model operation hooks to track data changes and update the related Depot.drivers_count property:

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

More information here Loopback Operation hooks - After Save

Upvotes: 0

F3L1X79
F3L1X79

Reputation: 2675

Looks like the datas are not converted to JSON.

I would try this:

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

Upvotes: 1

Related Questions