Call tRPC from the Next.js API folder

I'm working on calling an event that deletes old files when calld from a github actions cron job. this is the code:
import type { NextApiRequest, NextApiResponse } from "next";
import { api } from "../../utils/api";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === "POST") {
console.log("Cron job started");
const { authorization } = req.headers;

if (!authorization) return res.status(401).json({ success: false });

try {
const result = await api.cron.deleteOldTracks(authorization);
res.status(200).json({ success: true, result });
} catch (err) {
if (err instanceof Error) {
res.status(500).json({ success: false, message: err.message });
}
}
} else {
res.setHeader("Allow", "POST");
res.status(405).end("Method Not Allowed");
}
}
import type { NextApiRequest, NextApiResponse } from "next";
import { api } from "../../utils/api";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === "POST") {
console.log("Cron job started");
const { authorization } = req.headers;

if (!authorization) return res.status(401).json({ success: false });

try {
const result = await api.cron.deleteOldTracks(authorization);
res.status(200).json({ success: true, result });
} catch (err) {
if (err instanceof Error) {
res.status(500).json({ success: false, message: err.message });
}
}
} else {
res.setHeader("Allow", "POST");
res.status(405).end("Method Not Allowed");
}
}
it keeps giving me this problem at deleteOldTracks: This expression is not callable. any suggestions on a fix?
4 Replies
cornflour
cornflour•2y ago
what i learnt from the other day https://trpc.io/docs/server-side-calls
Server Side Calls | tRPC
You may need to call your procedure(s) directly from the server, createCaller() function returns you an instance of RouterCaller able to execute queries and mutations.
cje
cje•2y ago
api doesnt exist in the backend you can use callers, but depending on your exact use case theres a good chance its better to just extract a function and call that
cje
cje•2y ago
Christopher Ehrlich
YouTube
Advanced tRPC - Callers, functions, and gSSP
Repo for this video: https://github.com/c-ehrlich/you-dont-need-callers If you want to use schema in the frontend, they cannot be imported from the same file as things that run specifically in the backend. One solution would be to put them into a procedureName.schema.ts or similar file. tRPC: https://trpc.io/docs Create T3 App: https://crea...
Forsto
ForstoOP•2y ago
oh thanks for the help 😄
Want results from more Discord servers?
Add your server