NextJS route.ts gets a 504 Gateway Timeout

Here is my code, I am basically fetching some data from mongodb and get this error. Everything works fine in my local build, but not while hosted on vercel. Here s my code
import {getEncryptedTransactions} from '@/lib/actions/restapi.actions'
import { NextRequest } from 'next/server'
export const GET = async (req: NextRequest, res: Response) => {
try {
const auth = req.headers.get('Authorization')

if (auth === null) {
return new Response(JSON.stringify({
error: 'You did not add a Authorization header.'
}), {status: 404, headers: {
'Content-Type': 'application/json'
}})
}

const limit = Number(req.nextUrl.searchParams.get('limit') || 10)

const transactions = await getEncryptedTransactions(auth, limit)

return new Response(JSON.stringify(transactions), {status: 200, headers: {
'Content-Type': 'application/json'
}})
} catch (e: unknown) {
return new Response(JSON.stringify({
error: e instanceof Error ? e.message : 'An unknown error happened.'
}), {status: 500, headers: {
'Content-Type': 'application/json'
}})
}
}
import {getEncryptedTransactions} from '@/lib/actions/restapi.actions'
import { NextRequest } from 'next/server'
export const GET = async (req: NextRequest, res: Response) => {
try {
const auth = req.headers.get('Authorization')

if (auth === null) {
return new Response(JSON.stringify({
error: 'You did not add a Authorization header.'
}), {status: 404, headers: {
'Content-Type': 'application/json'
}})
}

const limit = Number(req.nextUrl.searchParams.get('limit') || 10)

const transactions = await getEncryptedTransactions(auth, limit)

return new Response(JSON.stringify(transactions), {status: 200, headers: {
'Content-Type': 'application/json'
}})
} catch (e: unknown) {
return new Response(JSON.stringify({
error: e instanceof Error ? e.message : 'An unknown error happened.'
}), {status: 500, headers: {
'Content-Type': 'application/json'
}})
}
}
Could it be because of it being slow?
1 Reply
xSenny_
xSenny_OP5mo ago
'use server'

import { auth } from "@clerk/nextjs/server";
import { getTransactionsCached } from "./transaction.actions";
import Transaction from "../database/models/transaction.model";

const CryptoJS = require('crypto-js');

const SECRET_KEY = process.env.SECRET_KEY;

const decrypt = (encryptedText: string) => {
const bytes = CryptoJS.AES.decrypt(encryptedText, SECRET_KEY);
return bytes.toString(CryptoJS.enc.Utf8);
}

export const getEncryptedTransactions = async (encryptedKey: string, limit: number = 10) => {
try {
const user = decrypt(encryptedKey)
const transactions = await Transaction.find({user}).sort({createdAt: 'desc'}).limit(limit).select('-_id -user -__v').exec()
return {transactions}
} catch (e) {
throw new Error('Error receiving your transactions. Make sure you use the correct Authorization value.')
}
}
'use server'

import { auth } from "@clerk/nextjs/server";
import { getTransactionsCached } from "./transaction.actions";
import Transaction from "../database/models/transaction.model";

const CryptoJS = require('crypto-js');

const SECRET_KEY = process.env.SECRET_KEY;

const decrypt = (encryptedText: string) => {
const bytes = CryptoJS.AES.decrypt(encryptedText, SECRET_KEY);
return bytes.toString(CryptoJS.enc.Utf8);
}

export const getEncryptedTransactions = async (encryptedKey: string, limit: number = 10) => {
try {
const user = decrypt(encryptedKey)
const transactions = await Transaction.find({user}).sort({createdAt: 'desc'}).limit(limit).select('-_id -user -__v').exec()
return {transactions}
} catch (e) {
throw new Error('Error receiving your transactions. Make sure you use the correct Authorization value.')
}
}
here s my entire file where I get the data from
Want results from more Discord servers?
Add your server