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