Skip to content

In loopback 4, Hasmany relation returns undefined instead of empty array when there is no related data #592

Open
@nehemiekoffi

Description

@nehemiekoffi

Steps to reproduce

Query data by including hasmany relation..

// Exemple
result = await this.authorRepository.findOne({ where: { id : '<authorId>'}, include:['books']  })

Current Behavior

in the result, you will notice that books is undefined for authors who dont have any books.

[
  {
  id : 'xxxx',
  name : 'abc',
  # books is undefined 
 },
 {
  id : 'xxxx',
  name : 'abc',
  books : [
  {
    id : 'xxxxx',
    title : 'zzzzz'
   }
  ]
 }
]

Expected Behavior

Books should return empty array for authors who don't have books. it was like that in loopback 3.

[
  {
  id : 'xxxx',
  name : 'abc',
  books : [] #should be like that
 },
 {
  id : 'xxxx',
  name : 'abc',
  books : [
  {
    id : 'xxxxx',
    title : 'zzzzz'
   }
  ]
 }
]

Link to reproduction sandbox

Additional information

Related Issues

See Reporting Issues for more tips on writing good issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions