#Getting Promise Rejection Error for Fastify

38 messages · Page 1 of 1 (latest)

zenith wing
#

Hi guys,

im facing the following error on my production system:

<rejected> TypeError: Cannot read property 'preSerialization' of undefined
at preserializeHook (/node_modules/fastify/lib/reply.js:461:28)
at Reply.send (/node_modules/fastify/lib/reply.js:190:7)
at FastifyAdapter.reply (/node_modules/@nestjs/platform-fastify/adapters/fastify-adapter.js:167:29)
at ExceptionsHandler.catch (/node_modules/@nestjs/core/exceptions/base-exception-filter.js:28:28)
at ExceptionsHandler.next (/node_modules/@nestjs/core/exceptions/exceptions-handler.js:16:20)
at /node_modules/@nestjs/core/router/router-proxy.js:13:35
} reason: TypeError: Cannot read property 'preSerialization' of undefined
at preserializeHook (/node_modules/fastify/lib/reply.js:461:28)
at Reply.send (/node_modules/fastify/lib/reply.js:190:7)
at FastifyAdapter.reply (/node_modules/@nestjs/platform-fastify/adapters/fastify-adapter.js:167:29)
at ExceptionsHandler.catch (/node_modules/@nestjs/core/exceptions/base-exception-filter.js:28:28)
at ExceptionsHandler.next (/node_modules/@nestjs/core/exceptions/exceptions-handler.js:16:20)
at /node_modules/@nestjs/core/router/router-proxy.js:13:35```

I tried catching it and logging the stack trace but it always defaults to this error message. I'm seriously lost here, can anybody point me in the right direction?
violet swift
#

Can you replicate this locally, or is it only in production?

zenith wing
#

Only in production, when i spin up my local environment and do the same API request with the same body nothing happens

violet swift
#

Hmm, that's gonna make debugging this difficult

#

All endings or just a specific one?

zenith wing
#

That's why I'm asking if anyone has ideas 😄

#

Just a specific endpoint

violet swift
#

Hmm. Can you share that endpoint's code?

zenith wing
#

Not really unfortunately. It's a) much happening there in case of events and db and b) bound by NDA

violet swift
#

Yeah it's more of just wanting to check for all promises going on, but I get it

zenith wing
#

I tried this

        console.log('Unhandled Rejection at: Promise', p, 'reason:', error);
        console.dir(error.stack);
        // application specific logging, throwing an error, or other logic here
    });```

but it's still the same error. SO pointed me to that to see the underlying promise that is failing but no change in output
violet swift
#

It looks like from the stack trace the way Reply is created/used is incorrect in the adapter. The_only_ way I could see that happening is if somehow you possibly have different fastify versions

zenith wing
#

i indeed have to fastifys in my package.json but i dont know why

#
        "@nestjs/platform-fastify": "^9.1.4",```
violet swift
#

@fastify/static is a plugin for fastify. @nestjs/platform-fastify is the http adapter package to make nest work with fastify. Neither of them are fastify itself

zenith wing
#
        "fastify-multipart": "^5.4.0",```

these two are for image handling
#

no more fastify packages in package.json

violet swift
#

Is this an endpoint that is handling multipart data?

zenith wing
#

No

#

It's an endpoint for sending out an email

#

But it fails even before we get to the sending

violet swift
#

It's weird that this looks like a double rejected promise too. I've not seen something like this before

zenith wing
#

why double?

violet swift
#

Unhandled Refection at: Promise Promise { stack trace } stack trace

violet swift
#

Ah, okay.

zenith wing
#

Do you know that this preSerialization hook does anyways?

violet swift
#

Based on the name, gets the response ready to be serialized. Do I actually know? No

zenith wing
#

😄

#

Can this be a frontend error when a wrong content-type is selected?

violet swift
#

Possibly? Most of the time it's just a JSON response

zenith wing
#

so we tried with application/json from the frontend as well and it put out the same error

violet swift
#

That's super weird. I'd need some code that reproduces that to possibly get a better understanding of what's going on

zenith wing
#

WE FOUND IT

#

omg

#

The frontend send the authorization header like this: "Bearer " because the accesstoken was null on the frontend because it was not needed for the api

#

fastify fails there

#

when bearer is send with empty token