loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Empty relation should return empty array

Open razyon opened this issue 3 years ago • 0 comments

Describe the bug

Hi,

I'm migrating a project from loopback 3 to loopback 4 and I found inconsistencies between the two regarding relations.

I am querying a model which have a HasMany relation on another model like this:

export class User extends Entity {
 ...

  @hasMany(() => Order)
  orders: Order[];

 ...
}

The query looks like this: /users?filter={"include": ["orders"]}

In the answer, I got an orders property for users that have orders but no orders property for users that don't. Here is an API response for example:

[
  ...
  {
    "id": "__",
    "email": "[email protected]",
  },
  {
    "id": "__",
    "email": "[email protected]",
    "orders": [
      {...}
    ]
  },
  ...
]

Is this an intended behavior in loopback 4 ? I find this much harder to deal with undefined property instead of empty array when querying model with relation.

Best regards

Logs

No response

Additional information

No response

Reproduction

https://codesandbox.io/s/falling-frost-qn03xh?file=/src/models/user.model.ts

razyon avatar Sep 13 '22 14:09 razyon