Having trouble accessing and establishing a connection with MongoDB

I've been working on a mini URL shortener project using JavaScript, Express, and MongoDB, but I've encountered some errors when trying to launch my local server and connect to MongoDB! Here's a snippet of my code:

const express = require('express')
const mongoose = require('mongoose')
const ShortUrl = require('./models/shortUrl')
const app = express();

mongoose.connect('mongodb://localhost/urlShortener', {
useNewUrlParser: true, useUnifiedTopology: true
})

app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended: false}))

// More code here...

app.listen(process.env.PORT || 5000);

Here is the detailed log of the issue I'm facing:

[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
C:\Users\apsoltanian-pc\Documents\js\url-shortener\node_modules\mongoose\lib\connection.js:807
  const serverSelectionError = new ServerSelectionError();
                               ^
// More error details here...

Node.js v17.7.2
[nodemon] app crashed - waiting for file changes before starting...

Any help would be greatly appreciated. Thank you!

I'm facing difficulties connecting to my MongoDB database while trying to run my JavaScript, Express, and MongoDB based URL shortener project. Any advice or assistance would be highly appreciated. Thank you!

Answer №1

After some troubleshooting, I managed to resolve the issue by substituting localhost with 127.0.0.1 in the connection string. The problem turned out to be related to certain ipv6 configurations... Thank you! :)

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

Exploring the capabilities of VueJs in detecting events triggered by parent components

When users click on a specific image in the Collection(parent component), my goal is to display that image in a Modal(child component). Below is the code snippet: routes.js import Home from './components/Home'; import About from './compone ...

Best practices for retrieving information from the user interface

My journey with NextJS has been a learning experience as I navigate connecting the frontend to the backend. Initially, I attempted to create a Restful API for CRUD operations from the frontend, but encountered authentication issues that left me puzzled. A ...

The Mongoose search query is failing to return an array

I encountered an issue recently. When I retrieve data from a database (using mongoose) with the .find() method, it returns objects separated by commas instead of an array. I actually need it to be in array format so that I can loop through it to map and di ...

Revamp the layout of the lower HTML video menu

Here is my code: <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> video { width: 100%; height: auto; } </style> </head> <body> <video width="400" controls> ...

Unable to access Session state in Web Service through ajax on the client side

I'm facing an issue where the system is generating a new session for each user when I run an ajax query, instead of having just 1 session per user. Strangely, there were no issues when I tested it in debug mode on my computer, but problems arose on th ...

Issue with rendering in sandbox environment versus localhost

Here's a dilemma I'm facing - I have some HTML code on my localhost that looks like this: <div> <p>Go to: <a href="www.foobar.com">here</a></p> </div> On localhost, the output is "Go to: here" with 'he ...

Creating an original list by iterating through a given list

I understand that some may consider this a duplicate, but I have yet to come across a similar example that I can relate to with my limited knowledge. So, I apologize in advance :) This should be pretty simple for an experienced JS developer, I assume. My ...

Vanilla JS Rock, Paper, Scissors Game - Fails to reset classes upon restarting

Many questions have been raised about this particular topic, but a solution for vanilla js seems elusive. Let's delve into a new challenge: Rock paper scissors with vanilla js. The game functions properly on its own, but the issue arises when attemp ...

Transfer JSON data between controllers without utilizing Services or Factory in AngularJS during routing

On my Dashboard page, I have an Object. When a user clicks on the Details Page from the Dashboard, it should redirect to the Details page. I am looking to pass the JSON Object from the Dashboard Controller to the Details Controller. Is there a way to do ...

Unable to send email through Ajax/PHP contact form

It's quite amusing that it worked perfectly for one evening. After reaching out to my host, they assured me that there should be no issues with it not working. I even tried testing it in Firebug, but it appeared to be sending successfully. Additionall ...

I need help figuring out how to set the country code as uneditable and also display a placeholder in react-phone-input-2 with React

How can I display the placeholder after the country code without allowing it to be edited? Currently, it's not showing up in my React functional components. Here is the code snippet: <PhoneInput className="inputTextDirLeft" ...

Tips for adjusting the property of an object that has been added to an array?

I have created an object. { "heading": [{ "sections": [] }] } var obj = jQuery.parseJSON('{"header":[{"items":[]}]}'); Then I add elements to the sections var align = jQuery.parseJSON('{"align":""}'); obj["he ...

The attempted creation of the ui.tree module failed with the error: [$injector:nomod] Unable to find module 'moduleName'! This could be due to a misspelling of the module name or

In my AngularJS project, I am utilizing a third-party library called angular-ui-tree to display data. To integrate this library, I need to include ui.tree as a dependency in my main app module. Following the instructions provided in the link, I have succes ...

The ability to update a variable passed through the state in Link is restricted

Currently, I am in the process of updating the theme in my App using useState. The updated theme is passed to the Topbar Component as a prop, triggering console.log() every time it changes. The theme is then passed from the Topbar to the AboutMe Component ...

What are some methods for creating a Venn Diagram that includes data within each section using SVG, CSS, or Canvas?

I am attempting to replicate this visual representation using pure SVG, CSS, or Canvas drawing. So far, I have successfully created three circles that overlap and placed a label in the center of each one. However, I'm facing challenges when it comes t ...

having trouble with displaying the results on the webpage

I am facing an issue while trying to display the data fetched from the database: {"results": ["USA", "Canada"]} {"message":"Could not find any countries."} //else An error was encountered in the console: Uncaught TypeError: Cannot read property 'l ...

Express encounters an issue retrieving POST data

Currently, I am in the process of learning Node.js and the express.js framework through a tutorial. Following this tutorial, I successfully installed a front-end module from node and linked it to my back-end. However, despite my efforts, I found that I am ...

Next.js server component allows for the modification of search parameters without causing a re-fetch of the data

I have a situation where I need to store form values in the URL to prevent data loss when the page is accidentally refreshed. Here's how I am currently handling it: // Form.tsx "use client" export default function Form(){ const pathname ...

Which file format is most compatible for uploading into MongoDB?

As I am not well-versed with MongoDB, I apologize if my query sounds too basic. In my project, I deal with 4 datasets, each containing files related to samples. Every sample has files for three normalization methods. The total count of samples across all ...

Steps to create a slideup effect using JavaScript

Upon clicking the answer text area for the first time, a message box will appear. Your Answer Thank you for contributing an answer to this platform! Please remember that this is a Q&A site, not a forum for discussion. Include specifics and share you ...