TRPCError invalid_type

182 Views Asked by At

I'm working on a project that uses tRPC (a TypeScript-first RPC library) for handling CRUD operations in a TypeScript/Node.js environment. I've set up a router for a Mashup resource that includes operations for retrieving, creating, editing, and deleting mashups. The code seems fine to me, but when I try to execute certain operations

const getMashupsOutputSchema = z.object({
    id: z.string(),
    title: z.string(),
    content: z.string(),
    owner: z.string(),
    timestamps: z.object({
        createdAt: z.date(),
        updatedAt: z.date(),
    }),
});

I'm struggling to identify the root cause of this error. The error message mentions an issue with an expected "object" type, but I can't figure out where this is coming from.

Here's a snippet of the relevant code:


    createMashup: publicProcedure
        .input(
            z.object({
                title: z.string().min(1).optional(),
                content: z.string().min(1).optional(),
            })
        )
        .output(getMashupsOutputSchema)
        .mutation(async ({ input: { title, content } }) => {
            console.log('Input data:', { title, content });

            connectDB();
            const mashup = await MashupPage.create({
                title,
                content,
            });

            console.log('Created Mashup:', mashup);

            return mashup;
        }),

but i'm recieving this error

{
    "error": {
        "json": {
            "message": "[\n  {\n    \"code\": \"invalid_type\",\n    \"expected\": \"object\",\n    \"received\": \"undefined\",\n    \"path\": [],\n    \"message\": \"Required\"\n  }\n]",
            "code": -32600,
            "data": {
                "code": "BAD_REQUEST",
                "httpStatus": 400,
            }
        }
    }
}

I would appreciate any insights or suggestions on how to debug and resolve this issue. If there are any improvements or best practices related to tRPC and TypeScript that I might be missing, please let me know.

Thanks in advance!

0

There are 0 best solutions below