TeixeiraT
tRPC12mo ago
6 replies
Teixeira

`FST_ERR_REP_ALREADY_SENT` when using `res.send()`

I've setup context as such:
import { CreateFastifyContextOptions } from '@trpc/server/adapters/fastify';
import models from '../database/models.js';

interface CreateInnerContextOptions extends Partial<CreateFastifyContextOptions> {
    models: typeof models;
}

export async function createInnerContext(opts: CreateInnerContextOptions) {
    return {
        models: opts.models
    };
}

export async function createContext(fastifyOptions: CreateFastifyContextOptions) {
    const innerContext = await createInnerContext({ models });

    return { ...fastifyOptions, ...innerContext };
}

export type Context = Awaited<typeof createContext>;


And a procedure like this:
import generateCampaignReport from '../../../../xlsx_reports/generateCampaignReport.js';
import { publicProcedure } from '../../../init.js';
import objectIdSchema from '../../../zod_schemas/ObjectId.js';
import { z } from 'zod';
import { Types } from 'mongoose';

export default publicProcedure
    .input(
        z.object({
            campaignId: objectIdSchema
        })
    )
    .mutation(async ({ input, ctx }) => {
        const { campaignId } = input;
        const { res } = ctx;

        res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        res.header('Content-Disposition', `attachment; filename="clipx_workbook.xlsx"`);

        const buffer = await generateCampaignReport(new Types.ObjectId(campaignId));
        res.send(buffer);
    });
Solution
this looks like it should be an api endpoint outside of trpc
Was this page helpful?