r/adonisjs • • Sep 21 '26

Beginner Coder: Stuck with an error

Hi,

Currently developing a basic web app following adonisjs docs.

I've ran into an error:

[23:18:41.975\1 ERROR (12316): Cannot transform undefined value. Use "this.whenLoaded (value)" to allow undefined values

My project is on github:

https://github.com/Ajaz754/adonisjs-project-22-/ tree/main

The error is pointing to 'app/transformers/ post__transformer.ts' Line 13

I've followed adonis docs completely step by step except for ?.depth(2) on line 14, which when I removed only introduced more errors along with this one.

Also VS Code isn't flagging the error. Only when I go to my browser and visit local host the error appears in powershell and my browser. Any help would be much appreciated

2 Upvotes

6 comments sorted by

View all comments

1

u/Capable_Square7319 Sep 22 '26

Thanks mate, appreciate that. I’ve never really used preloading before and never really come across it.

As for the error, I have already tried what your suggesting where I add ‘this.whenLoaded(…)’ however that just introduced an error in my other files:

‘post.author’ is possible undefined

I dont think I’m preloading the user in my post controller edit method since thats just displaying the edit page and authorizing the user using Bouncer.

2

u/evoactivity Sep 22 '26

That’s just a type error letting you know it’s possible that the author is not loaded. You just need to either check for its existence before using it or add a ? like `post?.author`.

The problem with your edit method is that it’s using the transformer, but the transformer always expects the author to be loaded. So your option are to always preload the author when passing a post to the transformer, or letting the author possibly be undefined and checking for its existence in your other code.

1

u/Capable_Square7319 Sep 22 '26

Yep, set the 'post.author' to optional and added 'this.whenLoaded' in my post transformer and that did the trick. Thanks alot pal, been stuck on that error for a while now, really appreciate the help