Next.js Discord

Discord Forum

Deployment turning API's to static

Unanswered
Japanese pilchard posted this in #help-forum
Open in Discord
Japanese pilchardOP
Hey folks, I'm having an issue with my APIs. I'm trying to retrieve all data from the database using .find or $match without any arguments, but Vercel is turning these routes static. Can anyone help?

24 Replies

@gin wdym mean by that?
Japanese pilchardOP
Yes it's MongoDB.
are u sure ur instance is active before requesting
or do u create a new one every time u request
Japanese pilchardOP
Are you talking about data?
im talking about the mongo instance
Japanese pilchardOP
I didn't understand, I am new.
send me ur code
for your handler
Japanese pilchardOP
this is my code
are u using app router?
@gin are u using app router?
Japanese pilchardOP
Yes
whats the connectDB function u have
Japanese pilchardOP
It is to connect to the database.
const mongo = Mongo();

export async function GET(req: Request, res: Response) {
}
"use server";

import {MongoClient, Db} from "mongodb";

let client: MongoClient;
let dbInstance: Db;

async function getMongoClient(): Promise<MongoClient> {
  if (!client) {
    client = await MongoClient.connect("yourstring", {});
  }
  return client;
}

async function Mongo(): Promise<Db> {
  if (!dbInstance) {
    const client = await getMongoClient();
    dbInstance = client.db("yourdb");
  }
  return dbInstance;
}

export {getMongoClient, Mongo};
hope this helped u
this is how i do it
Japanese pilchardOP
Thanks, brother for help. Let me try
American Crow
GET endpoints are static by default in production
https://nextjs.org/docs/app/building-your-application/routing/route-handlers#caching

you'll have to opt out by doing one of these
its probably not defined and thats why it returns null
@gin im suspecting him having issues with his mongodb instance
American Crow
yea might be i am just wondering why that would be just in production other than wrong env variables