loopbackio / loopback-connector-mysql

Loopback Connector for MySQL
Other
125 stars 183 forks source link

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

Open nehemiekoffi opened 4 months ago

nehemiekoffi commented 4 months ago

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