loopbackio / loopbackio/loopback-connector-mysql
In loopback 4, Hasmany relation returns undefined instead of empty array when there is no related data
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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