elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.44k stars 223 forks source link

[Possible bug] Function from plugin throws undefined error at runtime #365

Open Nmans01 opened 10 months ago

Nmans01 commented 10 months ago

Haven't been able to get help with this in the discord server. When using functions from plugins that act as "service" files, such as the following, I sometimes find that an error is thrown at runtime, saying that the function is not a function. This happens only when some name is set for the plugin, and does not happen when the plugin name is undefined. At no point does the IDE indicate a TS error.

// listingService.ts
export const listingService = new Elysia({ name: "listingService" })
    .use(authService)
    .derive(({ prisma, getUser }) => ({

        getListing: async (id: string) => {
            const currentUser = await getUser();

            // ...

            const listing = await prisma.listing.findFirst({
                // ...
            });

            if (!listing) throw new NotFoundError("Listing not found");

            return listing;
        }
    }))

Error:

`getListing is not a function. (In 'getListing(apartmentId)', 'getListing' is undefined)`

I can provide more code for this specific case, but this unintended behavior is difficult to reproduce as it does not seem to happen consistently.

SaltyAom commented 2 months ago

Hi, would like to help. Could you provide a basic reproducible code that I can copy-paste on my end.

Thanks.