unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.89k stars 496 forks source link

NitroRoute Serialize Type #2583

Open Ayax0 opened 2 months ago

Ayax0 commented 2 months ago

Environment


Reproduction

https://stackblitz.com/edit/github-7q39gn?file=_working.ts&view=editor

Describe the bug

In my project I use MongoDB and accordingly the library mongoose. If I now want to return my mongoose response directly in an EventHandler, the object is then of type unknown.

I was able to trace the problem back to the Serialize type of nitro: https://github.com/unjs/nitro/blob/v2/src/types/fetch/_serialize.ts

Through some tests I was able to determine that the mongoose object runs through the function up to line 43 (ReadonlyArray) the function NonJsonPrimitive has a negative outcome and accordingly Serialize is called recursively. The second itteration is stopped on line 37 and the type is returned on line 38 and then corresponds to unknown.

When I comment out lines 37 and 38 the type is successfully resolved and everything works as desired. Unfortunately I don't know enough about typescript to be able to estimate how the problem can be solved.

Additional context

https://github.com/nuxt/nuxt/issues/27968

Logs

No response

Ayax0 commented 2 months ago

Okay, I have found a solution to this problem. I just have to execute the lean() function on the query object. Then the type is recognized correctly.

return await userDb.find().lean();

I don't know if it is possible to offer a solution here that also works otherwise. If not, this issue can be closed.