Skip to content

POST Request Fails with "Cannot read properties of undefined (reading 'length')" #4928

Closed Answered by kiddocoder
kiddocoder asked this question in Help
Discussion options

You must be logged in to vote

I finally found the real cause:

I realized that since Lucid ORM is built on top of Knex, the problem was coming from how the model relationships are handled internally.

It was a mistake inside a @computed property on one of my models (Budget model) of Banc Account.
I was directly accessing this.budgets.length without checking if budgets was actually loaded from the database first.

Because of that, AdonisJS was crashing during response serialization and any POST, GET, or random API call would cause 500 errors.

The fix was super simple:
I just had to safely check it like this:

@computed()
get budgetCounts() {
  return this.budgets?.length || 0
}

Special thanks 🙏 to you @RomainLanz for taki…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kiddocoder
Comment options

Answer selected by RomainLanz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants