Unable to access API route on Vercel, but it works fine on local
Unanswered
Bombay posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
Hello
I have api routes in my Next 13 application using the app router. For example, on my localhost, I can access this route just fine:
It returns the data requested.
When I push to main on github and it publishes successfully on Vercel, I get this response from the following endpoint.
My api route looks like this for a GET request:
Stack Details are:
MongoDB hosted on Atlas
Prisma ORM, client version 4.16.2
NextJS version 13.4.8
Why am I not able to fetch data on vercel but it works fine on local?
I have api routes in my Next 13 application using the app router. For example, on my localhost, I can access this route just fine:
http://localhost:3000/api/modeling/level/647fd6dd22a310243b687152
It returns the data requested.
When I push to main on github and it publishes successfully on Vercel, I get this response from the following endpoint.
/ 20230705074445
// https://griddle-6u6618xqe-joselvelez.vercel.app/api/modeling/level/647fd6dd22a310243b687152
{
"message": "level not found"
}
My api route looks like this for a GET request:
export const GET = async (request:NextRequest, {params}: {params: {id: string}} ) => {
try {
const level = await prisma.level.findUnique({
where: {
id: params.id
}
});
if (!level) {
return NextResponse.json({message: "level not found"}, {status: 404})
}
return NextResponse.json({data: level}, { status: 200 })
} catch (err) {
return NextResponse.json({ message: "GET error, no level found", err}, {status: 500})
}
}
Stack Details are:
MongoDB hosted on Atlas
Prisma ORM, client version 4.16.2
NextJS version 13.4.8
Why am I not able to fetch data on vercel but it works fine on local?
33 Replies
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
because id you passed is different
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
How so? the string at the end is a url param that is different depending on the record I am looking up. I am using the params object to access the id from the url. Did I miss something? It works find on localhost
ah! I see what you mean!
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
if you want to get answers, you show a simple example. keep in mind nobody know your impl
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
I just fixed that.. it still does not work. both of those ids are valid records in my mongodb
gotcha, sorry. I am new to vercel and next 13. Is there something specific I can provide? I dont want to provide my .env variables.
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
is that a dynamic page?
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
yes, inside my app dir, i have
app/api/modeling/level/[id]/route.ts
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126159796849098843/image.png)
Here is what it looks like on localhost
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126160115591037068/image.png)
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
add console.log after 9 to print params.id
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
and this is on vercel
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126160284193656832/image.png)
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126160492298248192/image.png)
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
and hard-code an ID that exists in DB. see the diff
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
on local I get this when hard coding an id
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126161196207321150/image.png)
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126161196605771846/image.png)
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
a u really sure it connects to the same DB?
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
how can I be sure that the vercel instance is connecting to the same DB? I am using their MongoDB integration. It was working fine before.
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
go to a Vercel Dashboard, check ENV of MongoDB endpoint
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
I have set my prisma datasource to use the same env variable for the MongoDB connection url on local and vercel
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126163308773384242/image.png)
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126163309054410802/image.png)
the Vercel MongoDB integration created those other 2 env variables, but i dont know if I need to do something with those.
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
at line 9 before findUnique(), add insert statement. then findUnique(the inserted doc) to see how it works.
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
should I do this on local or vercel?
on local I have an api endpoint that works and allows me to insert new records.
On local, using thunderclient, shows up on Mongo Compass
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126165132423213106/image.png)
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126165132767154226/image.png)
This is the route file for the create api endpoint
import { NextResponse } from "next/server";
import prisma from '@/app/libs/prismadb';
export const POST = async (request:Request) => {
try {
const body = await request.json();
const {
id,
level_name,
level_code,
level_desc,
inUse,
currency_select,
parentLevel_select,
currentClient,
currentInstance,
} = body;
const createLevel = async () => {
await prisma.level.create({
data: {
name: level_name,
code: level_code,
inUse: inUse,
clientId: currentClient,
instanceId: currentInstance,
currencyId: currency_select,
parentId: parentLevel_select,
description: level_desc,
updatedAt: new Date(),
createdAt: new Date(),
}
});
}
// Update new parent to reflect that it has a child and is in use
const updateNewParent = async () => {
await prisma.level.update({
where: {
id: parentLevel_select
},
data: {
inUse: true
}
});
}
try {
createLevel();
updateNewParent();
} catch (err) {
throw Error("unable to create level and udpate parent")
}
return NextResponse.json("new level created successfully")
} catch (err) {
return NextResponse.json({ message: "POST error", err}, {status: 500})
}
}
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
i guess it does not matter. can u accees it with MongoDB Compass?
and try another way like findFirst(). something wrong with Prisma cache.
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
Why do you think it is something with prisma cache? Just curious.
I think it has something to do with vercel deployment. I just dont know what. I'm new to vercel
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
i mean combination of Vercel and Prisma cache
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
The api endpoint is working bc I get back my custom message, but I dont know why it cant retrieve the record from the db.
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126167683294044250/image.png)
![Image](https://cdn.discordapp.com/attachments/1126139408421240923/1126167683700895764/image.png)
![Avatar](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
tafutada777
if i were u, i tried other function like findFirst, findMany to see how it works.
its up to u
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
BombayOP
K, I will try that and report back. Thanks for your help!