Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
My mongodb connection stopped working and just loading into infinity and then I get a dissconnected error

Hello I have been trying to fix this for a week. It worked at first, but now it just stopped. I want to connect my vue app to a mongodb hosted by them and using this code

const express = require("express");
const mongodb = require("mongodb");
const router = express.Router();
router.get("/", async(req, res) => {
    const gottenData = await doStuff();
    res.send(await gottenData.find({}).toArray());
});

async function doStuff() {
    console.log("connecting");
    const client = await mongodb.MongoClient.connect(
        "mongodb+srv://<NAME>:<PASSWORD>@cluster0.9tlzg.mongodb.net/database?retryWrites=true&w=majority", { useNewURLParser: true }
    );

    return client.db("database").collection("collection");
}

module.exports = router:

I am attaching screenshot of my mongodb setup as well

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should create a client and then .connect separately.

const express = require("express");
const mongodb = require("mongodb");
const router = express.Router();
router.get("/", async(req, res) => {
    const gottenData = await doStuff();
    res.send(await gottenData.find({}).toArray());
});

async function doStuff() {
    console.log("connecting");
    const client = new mongodb.MongoClient('mongodb+srv://<NAME>:<PASSWORD>@cluster0.9tlzg.mongodb.net/database?retryWrites=true&w=majority', { useNewURLParser: true });

    await client.connect();

    return client.db("database").collection("collection");
}

module.exports = router:

Edit: see this for docs https://docs.mongodb.com/drivers/node/current/fundamentals/connection/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!