Why am I encountering a 504 server error in the live environment while attempting to establish a connection with the database?

I feel like I'm losing my sanity. I've attempted this multiple times by now. Currently, I'm working on setting up an authentication flow using a project tutorial I stumbled upon on YouTube. The goal is to establish the flow with Next.js and Mongoose. Initially, I thought the issue might be related to using React server actions, so I switched to API calls instead, but that didn't solve the problem either.

Below is the basic registration form:

"use client";
import Link from "next/link";
import React, { useEffect } from "react";
import {useRouter} from "next/navigation";
import axios from "axios";
import { toast } from "react-hot-toast";

// Rest of the code goes here... (omitted for brevity)

This form interacts with the following API route:

import {connect} from "@/dbConfig/dbConfig";
import User from "@/models/userModel";
import { NextRequest, NextResponse } from "next/server";
import bcryptjs from "bcryptjs";

// Rest of the code goes here... (omitted for brevity)

Additionally, the script connects to the database as shown below:

import mongoose from 'mongoose';

export async function connect() {
    // Database connection setup...
}

I have configured environmental variables for the database, but despite this, the application only seems to work in a local environment. Uploading it to Vercel or Heroku results in failure. When deployed in production, the following error message is displayed:

Error message details...

Though I don't expect you to solve this complex issue for me, as I struggle to do so myself, good luck if you decide to take on the challenge. I've even tried creating a new Next.js project from scratch, implementing a basic registration form, server action, and API routes for testing. While everything functions smoothly in development, deploying to Heroku or Vercel keeps resulting in 504 or 503 errors. It's been quite a frustrating experience, to say the least.

Answer №1

Consider the following two scenarios:

  • Ensure all your environment variables are properly set up on your cloud platform (such as Vercel or Heroku). Pay special attention to the MONGO_URI variable, as having it configured locally doesn't guarantee it's also set in the production environment.

  • If everything checks out and you're utilizing MongoDB Atlas, verify if an access whitelist is enabled and make sure it includes your production IP address.

I trust these tips will be beneficial to you. Best of luck!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble displaying the favicon.ico in Nextjs?

In my current project, I am using Nextjs version 14.2.5 for the app directory. The favicon.ico has been placed correctly in the app file. Upon inspecting the elements, I can see the favicon link as shown in the image below. However, for some reason, i ...

Managing dialog activation using Radix Dialog from a different dialog

Hello frontend enthusiasts. I have been working on a NextJS web project that utilizes Shadcn components, which serve as an additional layer atop Radix UI. My current goal is to develop a page builder capable of dynamically loading components from Strapi ( ...

Failure to acknowledge the incoming request

I'm currently working on developing a new app that involves logging in and authenticating users. I have experience with another app called "doctors" where I successfully utilized Mongoose, Passport, and Passport-Local modules to achieve the desired fu ...

A guide on retrieving bytecode from a specific PDF using Angular

Can anyone help me with extracting the bytecode from a selected PDF file to save it in my database? I keep encountering an error stating that my byte is undefined. Could someone please review my code and identify what might be causing this issue? I attemp ...

Utilizing Angular 2 Service for Efficient Data Exchange Among Components

I'm experimenting with data sharing between components using a shared service. Here is the service: @Injectable() export class JobService { public jobType=null; public examples=[]; } My first component sets the jobType and examples variables ...

Tips for evaluating the stickiness of a block within a cell when it adheres to a mat-header-cell

I am working with an Angular table and facing an issue. How can I make the span element in the cells of the first column stick to the sticky mat-header-row when scrolling down the table? My requirement is for the span element to stay attached to the lower ...

Thinking about refreshing a page automatically when there is a change in data using Next.js?

After removing a note, it is deleted from the database. However, I have to refresh the page each time to see the change. Is there a way to view the remaining notes without having to reload the page? Below is the code for my current webpage: export default ...

Missing expected property in TypeScript casting operation

I recently came across this intriguing playground example outlining a scenario where I attempted to convert an object literal into an object with specific properties, but encountered unexpected results. class X { y: string; get hi(): string { ...

Is there a method in typescript to guarantee that a function's return type covers all possibilities?

In the case of having a constant enum like: enum Color { RED, GREEN, BLUE, } A common approach is to create a helper function accompanied by a switch statement, as shown below: function assertNever(x: never): never { throw new Error(`Unexpecte ...

Issue: Destination element is not a valid DOM element (React/Next.js)

I have been working on a React context component that displays a message across all pages in my application. Despite looking through various questions on stackoverflow, I am still encountering an error. Here is the code I have: message.js function Messag ...

Angular - Implementing a debounce feature for event handling

Currently, I am utilizing the mouseenter and mouseleave events to control the opening and closing of my sidenav within the app. However, I have encountered issues because when hovering over the container quickly, these events are triggered multiple times ...

Keeping track of various combinations of a string containing only certain characters

Currently, I am working on a project that involves replacing letters of the alphabet with numbers resembling similar styles in typescript. For example, converting the letter 'I' to '1'. I have successfully implemented a function called ...

Releasing Typescript 2.3 Modules on NPM for Integration with Angular 4

Although there are instructions available in Writing NPM modules in Typescript, they are outdated and there are numerous conflicting answers that may not be suitable for Angular. Additionally, Jason Aden has delivered an informative presentation on youtu ...

Combining normal imports with top-level await: A guide

Is it possible to simultaneously use imports (import x from y) and top-level awaits with ts-node? I encountered an issue where changing my tsconfig.compilerOptions.module to es2017 or higher, as required by top-level awaits, resulted in the following error ...

Displaying the specific error within the outcome message of Mongoose's Node validation

Currently, I am dealing with mongoose and axios within node.js/react to fetch the specific validation error on the client side in order to update the form. For instance, if the user email already exists in the database and is not unique, the error message ...

having difficulties with angular subscribing to an observable

I am currently working on a service that retrieves a post from a JSON file containing an array of posts. I already have a service in place that uses HttpClient to return the contents of a JSON file. The main objective is to display the full content of the ...

User.find in MongoDB returns an error message stating that the argument provided must either be a single String of 12 bytes or a string consisting of 24 hex characters

Below is the code snippet from my Express router: router.get('/user/events', verifyToken, (req, res) => { User.find({ _id: req.userId, 'signedToEvents.isActive': true }, (err, suc) => { if (err) { ...

What is the best way to analyze the current pathname and route in next.js?

Currently in the process of migrating a project developed with creat-react-app to a next.js setup. Encountered an issue while trying to migrate the matchPath function from react-router-dom to achieving a similar functionality in next.js, where we compare ...

The sequence of events in React Native following a navigation.navigate call

Seeking suggestions and advice, I currently have the following 2 lines of code within a react native expo component: this.props.navigation.navigate("App"); patchUser(this.state.dataSource.userInfo.username, this.state.dataSource.userInfo.firstN ...

What is the process for retrieving information from an observable array?

I am currently developing an application with a Python backend and Angular frontend. The main functionality of the app involves retrieving FIFA players from a MongoDB using the getLoyalPlayers function. Below is the snippet from the loyalty.component.ts f ...