POST Request Fails with "Cannot read properties of undefined (reading 'length')" #4928
-
I’m currently facing a serious issue with AdonisJS that I haven’t been able to solve. On my backend API, POST requests used to work perfectly, but now every POST request triggers this error:
Along with a 500 Internal Server Error and a FATAL error message:
🧩 Additional information: Strangely, even though the API sends a 500 Internal Server Error, a new record sometimes still gets created in the database after a short delay. I also noticed that even some GET requests can randomly cause the same crash, resulting in the " I checked the HTTP headers: Request Headers show Content-Length: 54 Response Headers show Content-Length: 58 When I log the request body, it appears as an object, not plain text.
Requests are sent with the correct Content-Type: application/json header. No obvious mistakes in the controller code (example: using request.only() with correct fields). Despite these checks, the API keeps crashing and making the server unstable after handling requests. ❓ My Questions: Could this be related to how AdonisJS handles response serialization internally? Could the What are the best practices to debug and fix Response serialization and server crashing issues like this in AdonisJS? This situation is very stressful 😓 and blocks me from continuing my project. Any help or insights would be highly appreciated 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @kiddocoder! 👋🏻 In order to help you, it would be best if you could share some code to reproduce your issue. |
Beta Was this translation helpful? Give feedback.
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:
Special thanks 🙏 to you @RomainLanz for taki…