I have a monorepo appliction using turborepo, the main web app is Nextjs and the database is accessed/handled by prisma. For 99% of the data retrieval we use trpc to get data from the database but we have to have one exposed HTTP REST endpoint and for this I thought to use a Next API route.
When I run my api route locally everything works perfectly however when I deploy to vercel I get a weird error. Basically the api route in next uses code from the shared packages/api directory. In that api directory we make all our prisma queries. The code runs great until it hits the first prisma query then ends the function without error.
It feels like the code isn't awaiting the prisma call?
I don't have a public Git repo to share so I will try to make a copy of what is happening below.
we are using this project as our base https://github.com/clerk/t3-turbo-and-clerk
app layout
apps
├─ expo
└─ next.js
├─ Next.js 14
├─ React 18
└─ E2E Typesafe API Server & Client
packages
├─ api
| └─ tRPC v10 router definition
└─ db
└─ typesafe db-calls using Prisma
And example of the api route
import {processedRequest} from '@acme/api/processed';
export default async function handler(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
console.log("got called here");
if (req.method !== "POST") {
return res.status(405).send({ message: "Only POST requests allowed" });
}
const processed = await processedRequest(req.body)
if(!processed) {
console.log("failed to process");
return res.status(500).send({message: "failed to process"});
}
console.log("Processed!)
return res.status(200).send({message: "ok"});
}
processed.ts
export async function processedRequest(body: BodyType) {
try{
console.log("Got to processing");
const prismaReturn = await prisma.table.count({
where: {
id: body.id
}
});
if(prismaReturn > 0){
console.log("We got something!", prismaReturn);
return true;
}
console.log("Failed to fetch");
return false;
} catch(error){
console.log("there was an error!", error);
return false;
}
}
Now if I hit our endpoint the logs that will come out will be the following:
got called here
Got to processing
failed to process
It seems to hit the prisma query then just skip everything else after that.
This may be an issue with Vercel but any insight would be helpful. It feels like I am just off with my implementation.
Again the code works as desired locally but not on vercel for some reason. Thanks for any help!
For anyone seeing this, I had a forEach loop that executed an asynchronous function inside of it. This is not something you can do in javascript so it kicked off my prisma query but then just executed before prisma could respond.
This didn't happen when I tested locally because the database was on the same bare metal and was fast enough to respond in time.