Encountering difficulties: Payload CMS is unable to establish connection with mongo DB

Struggling to set up Payload CMS. When running npm run dev, I encountered an error stating:

ERROR (payload): Error: cannot connect to MongoDB. Details: URI does not have hostname, domain name and tld [nodemon] app crashed - waiting for file changes before starting...

I installed Mongo locally on my machine, choosing the option to run it as a Network Service User and skipping Mongo Compass installation. During Payload's setup, I provided the path to the Mongo database. After creating a new database in Mongo, I selected the "connect to your app" option and copied this snippet:

mongodb+srv://my_db_name:@my_name.something.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

I pasted this snippet where Payload requested it. In my project, there is a .env file which looks like this:

MONGODB_URI = mongodb+srv://my_db_name:my_password@my_name.something.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
PAYLOAD_SECRET = my_super_long_secret_key_thats_not_this

Although I added quotes around the values thinking it might be necessary, I still got the same error. The URI in the .env file contains all required information according to the docs. Following the instruction in the documentation, I tried running node server.js, or in my case server.ts since I'm using TypeScript, but it did not solve the issue. Watching a video tutorial by the creator, I noticed they used yarn dev, so I attempted npm run dev to start the server successfully until the Mongo connection failed. Any ideas on what could be going wrong?

Answer №1

Switch localhost with 127.0.0.1

Illustration:
postgresql://localhost/ -> postgresql://127.0.0.1/

Answer №2

Have you utilized the npx create-payload-app command to create your app? If so, it's recommended to launch the app using npm run dev. The development script contains the PAYLOAD_CONFIG_PATH variable which can be found in the package.json file.

You mentioned that you are using a local MongoDB installation, but your MONGODB_URI resembles an address for MongoDB Atlas service.

  1. For MongoDB Atlas connection - If this is the case, ensure that you have allowed access from your IP address to MongoDB Atlas by following the steps in the Atlas connection guide

  2. Regarding Local MongoDB installation - If you are certain about using the local database, then your address should look something like this:

    mongodb://root:example@localhost:27017

To manage and perform connection tests on your local MongoDB, consider utilizing MongoDB Compass application as it provides a user-friendly graphical interface.

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

What is causing all Vuejs requests to fail in production with the error message "javascript enabled"?

My vuejs application interacts with a REST API in Node.js (Express, MongoDB Atlas). Everything runs smoothly when I run the Vue app on localhost while the Node.js app is on the server. However, when I deploy my dist folder to the server, although the app ...

What are the benefits of utilizing a Database-as-a-Service such as mLab over running MongoDB on a local environment

While learning the MEAN Stack, I primarily utilized mlab for the small apps I created, although I have not yet used it for a production-level web application. ...

The method of having two consecutive subscribe calls in Angular2 Http

Can the Subscribe method be called twice? I am attempting to create an API factory that stores data in the factory and allows different components to use that data for each AJAX call. The factory: export class api { result = []; constructor (p ...

Skipping the Delete Route in Node.js with MongoDB

I'm currently facing an issue with my delete route in my app. My get and post routes are functioning perfectly, but the delete route is causing some trouble. On the front end, I've set up my JavaScript code to dynamically create HTML using templ ...

MongoDB table collections (table names in other databases)

After setting up my express server to connect to mongodb, I encountered an issue despite everything working fine initially. I created a collection in my mongodb called projects (plural form). In my project.model.js file, I defined the model as follows: c ...

The Vitest test is not compatible with PrimeVue3 Dialogs and does not function as intended

I am currently working on a project that involves using PrimeVue components, and the time has come to conduct some tests. Below is the code for the test: import { beforeEach, describe, expect, it } from 'vitest' import type { VueWrapper } from & ...

Assign a variable to the key of a statically typed object in TypeScript

Is there a cleaner way to explicitly type an object and assign key-value pairs where the keys are variable names? I found a solution using ES2015 template literals and JSON.parse() that looks like this: interface MyObj { [propName: string]: number; } ...

Determine overlapping ranges in JavaScript by comparing the "from" and "to" variables for

While working on my angular app, I encountered a seemingly simple task that turned out to be surprisingly complex. I have come up with a few solutions, but none of them seem perfect. The issue at hand is that I have an array containing possible unlimited ...

The MongoDB GridFs is throwing an error: "grid.mongo.ObjectID is not a valid constructor"

Currently, I am utilizing MongoDB GridFS, Node.js, and Express in my project. However, I have encountered an error message stating "TypeError: grid.mongo.ObjectID is not a constructor" when attempting to retrieve a file from the database. UploadController ...

Execute a function when an RXJS Observable has completed its operation

Using the RXJS Observable has been smooth sailing so far, but I now find myself needing to not only react to observer.next() but also when observer.complete() is called. How can I capture the OnComplete event of an RXJS Observable? The documentation for ...

Distinguishing Literal and Parameterized Routes in Angular 6

I've encountered a strange issue that I'm not sure how to handle. In my application, you can either view your public account or create a new one. The Account and CreateAccount modules are standalone and lazy loaded in the routes.ts file. export ...

Convert YAML to an array of objects instead of using named objects in npm parsing

Currently, I am utilizing npm's YAML parser to convert YAML into an object. However, instead of getting an array, I am receiving a group of named objects. This issue arises from the absence of dashes preceding the objects. How can I transform this gr ...

Exploring in Angular 2 by using First Name, Last Name, and Email for queries

Details Currently, I am working on implementing a search functionality using pipes. Users should be able to search by email, first name, or last name. At the moment, it only works for searching by email. I am looking to extend this capability so that user ...

Excessively Running Firebase Function Due to setInterval Usage

My Firebase function is set to run every 20 minutes using setInterval, but it seems to be executing more frequently than expected. Here is an excerpt from my code: try { const response = await axios.get( "https://ll.thespacedevs.com/2.0.0/ ...

Having trouble rendering data from Mongodb onto my Jade page

Below is the route I have set up : app.get('/dashboard', passportConf.isAuthenticated, userController.getBoardsCount,userController.getSharedBoardsCount, userController.getBoards); Here is the middleware code used to fetch data from MongoDB: e ...

What could be the reason for the cast error I'm encountering while using the populate() function in mongoose?

I encountered a perplexing error in my code that I need help troubleshooting: "message": "I have the following error appearing through my .catch and I'm having trouble figuring it out:", "name": "Code Confusion", "stringValue": "\"{\n type ...

Switch app engines in real-time based on the URL path with express framework

How can I dynamically set App Engine based on the URL? In my application, I have two render engines available: serverSideRenderEngine & browserRenderEngine If the URL is /home, the app.engine should be set as serverSideRenderEngine If the URL is /l ...

Executing an infinite loop can occur when employing an NgRx call to dispatch an action within an Angular router

Story: Greetings to all! As a newcomer to using the NgRx library, I have encountered an issue in my Angular project where I am utilizing a router guard to prevent unauthorized users from accessing certain parts of the website. My approach involves callin ...

How is it that TypeScript still expects a valid return from an overridden method, even when it cannot be reached?

After creating a generic RESTClient with some abstract functions, I encountered an issue where not all REST actions are implemented. In these cases, I wanted to throw an error. The problem arose when trying to override the TypeScript method to both return ...

The return type of TypeScript functions is not being properly documented

Can you help me understand why the result object is showing as type {foo: unknown, bar: unknown, baz: unknown}, when I believe it should actually be of type {foo: number, bar: boolean, baz: string} export function apply<A, B extends {[P in keyof A]: B ...