0

Hey while following this tutorial https://youtu.be/TRCDsB9i3bI and trying to get mongoDB to work I am coming across this problem. I am trying to set up an API on the backend at http://localhost:5000/api/users/seed in order to access the database using this code to send data to it.

import express from 'express'
import expressAsyncHandler from 'express-async-handler'
import data from '../data.js'
import User from '../models/userModel.js'

const userRouter = express.Router()

userRouter.get(
    '/seed', 
    expressAsyncHandler(async (req,res) =>{
        
            await User.remove({});
            const createdUsers = await User.insertMany(data.users)
            res.send({createdUsers})
        
}))

export default userRouter;

However I receive this error on the server:

"Operation `users.remove()` buffering timed out after 10000ms"

This is what I get in the terminal:

(node:9110) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (/Users/charlielamb/Desktop/learnCode/amazona/node_modules/mongoose/lib/connection.js:846:32)
    at /Users/charlielamb/Desktop/learnCode/amazona/node_modules/mongoose/lib/index.js:351:10
    at /Users/charlielamb/Desktop/learnCode/amazona/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/Users/charlielamb/Desktop/learnCode/amazona/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/Users/charlielamb/Desktop/learnCode/amazona/node_modules/mongoose/lib/index.js:1149:10)
    at Mongoose.connect (/Users/charlielamb/Desktop/learnCode/amazona/node_modules/mongoose/lib/index.js:350:20)
    at file:///Users/charlielamb/Desktop/learnCode/amazona/backend/server.js:8:10
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:177:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9110) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9110) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Has anyone get any suggestions as to how I can fix this as it seems no one else who followed the tutorial gets the same problem as me.

Thanks

charlietlamb
  • 79
  • 1
  • 1
  • 9
  • check the DB connection – Nikita Mazur Jun 18 '21 at 10:20
  • How would I do that? – charlietlamb Jun 18 '21 at 10:25
  • What does the `User.remove({})` do functionally? If you are deleting all documents in the collection - try dropping the collection - it is more efficient. – prasad_ Jun 18 '21 at 10:26
  • You are getting ECONNREFUSED so you are not connected to the db. Make sure you have correct url, port, username etc. – Molda Jun 18 '21 at 10:29
  • ```User.remove({})``` was in the tutorial, I think it was to remove previous data so that new data could be created. – charlietlamb Jun 18 '21 at 10:58
  • This is my connection code: ```mongoose.connect( process.env.MONGODB_URL || 'mongodb://localhost:5000/amazona',{ useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, })``` This is the same as in the tutorial so I'm not sure why it doesn't work – charlietlamb just now Edit Delete – charlietlamb Jun 18 '21 at 10:59
  • I'm assuming that you are developing on your computer so if you are trying to connect to mongo on localhost you have installed the mongo on your computer, right?? It doesn't magically install automaticaly. – Molda Jun 18 '21 at 11:07
  • yes I have mongo community edition installed. I just reinstalled it and it still does not work. ```Warning: mongodb/brew/mongodb-community 4.4.5 is already installed and up-to-date.``` – charlietlamb Jun 18 '21 at 11:16
  • This is what helped me solve my error https://developer.mongodb.com/community/forums/t/error-couldnt-connect-to-server-127-0-0-1-27017/705 i didnt run ```brew services start mongodb-community@4.4``` initially – charlietlamb Jun 18 '21 at 11:41
  • I am guessing if mongo is propperly installed. the missing part will be its daemon process. try following the steps 4 mentioned [here](https://stackoverflow.com/questions/20796714/how-do-i-start-mongo-db-from-windows?answertab=votes#tab-top). for [reference](https://docs.mongodb.com/manual/reference/program/mongod/) – rohanraj Jun 18 '21 at 11:47

0 Answers0