loopbackio / loopbackio/loopback-connector-mysql

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

Open
#592 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
126
Forks
181
Avg merge
4d 7h
Merged PRs (30d)
12

Description

## Steps to reproduce

Query data by including hasmany relation..
```ts
// Exemple
result = await this.authorRepository.findOne({ where: { id : ''}, include:['books'] })
```
## Current Behavior

in the result, you will notice that books is undefined for authors who dont have any books.
```yaml
[
{
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.
```yaml
[
{
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](http://loopback.io/doc/en/contrib/Reporting-issues.html) for more tips on writing good issues_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the authorRepository.findOne call using include: ['books'] and reproduce it with a MySQL relation where no related books exist. Trace how the hasMany include result is assembled, then verify that authors without related rows receive books: [] while authors with books retain their related records.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mysql, nodejs
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.