peachpops
peachpops

Reputation: 37

UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server

I'm trying to run a node seed.js file in the terminal to connect my project the mongodb backend server, but keep receiving the following error:

    UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  name: 'MongoNetworkError',
  message: 'connect ECONNREFUSED 127.0.0.1:27017',
  stack: 'Error: connect ECONNREFUSED 127.0.0.1:27017\n' +
    '    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)'
}]
    at Pool.<anonymous> (/Users/caseydzuong/Desktop/movierama/vidly-api-node/node_modules/mongodb-core/lib/topologies/server.js:503:11)
    at Pool.emit (events.js:315:20)
    at Connection.<anonymous> (/Users/caseydzuong/Desktop/movierama/vidly-api-node/node_modules/mongodb-core/lib/connection/pool.js:326:12)
    at Object.onceWrapper (events.js:422:26)
    at Connection.emit (events.js:315:20)
    at Socket.<anonymous> (/Users/caseydzuong/Desktop/movierama/vidly-api-node/node_modules/mongodb-core/lib/connection/connection.js:245:50)
    at Object.onceWrapper (events.js:422:26)
    at Socket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:84:8)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
    (node:3807) 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:3807) [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.

I've already tried whitelisting my IP address by adding '0.0.0.0/0' to the whitelist; it didn't work. What else can I do?

Here's the seed.js file:

const { Genre } = require("./models/genre"); const { Movie } = require("./models/movie"); const mongoose = require("mongoose"); const config = require("config");

const data = [ { name: "Comedy", movies: [ { title: "Airplane", numberInStock: 5, dailyRentalRate: 2 }, { title: "The Hangover", numberInStock: 10, dailyRentalRate: 2 }, { title: "Wedding Crashers", numberInStock: 15, dailyRentalRate: 2 } ] }, { name: "Action", movies: [ { title: "Die Hard", numberInStock: 5, dailyRentalRate: 2 }, { title: "Terminator", numberInStock: 10, dailyRentalRate: 2 }, { title: "The Avengers", numberInStock: 15, dailyRentalRate: 2 } ] }, { name: "Romance", movies: [ { title: "The Notebook", numberInStock: 5, dailyRentalRate: 2 }, { title: "When Harry Met Sally", numberInStock: 10, dailyRentalRate: 2 }, { title: "Pretty Woman", numberInStock: 15, dailyRentalRate: 2 } ] }, { name: "Thriller", movies: [ { title: "The Sixth Sense", numberInStock: 5, dailyRentalRate: 2 }, { title: "Gone Girl", numberInStock: 10, dailyRentalRate: 2 }, { title: "The Others", numberInStock: 15, dailyRentalRate: 2 } ] } ];

async function seed() { await mongoose.connect(config.get("db"));

await Movie.deleteMany({}); await Genre.deleteMany({});

for (let genre of data) { const { _id: genreId } = await new Genre({ name: genre.name }).save(); const movies = genre.movies.map(movie => ({ ...movie, genre: { _id: genreId, name: genre.name } })); await Movie.insertMany(movies); }

mongoose.disconnect();

console.info("Done!"); }

seed();

Upvotes: 0

Views: 1368

Answers (1)

Prathap Reddy
Prathap Reddy

Reputation: 1739

The error seems self explanatory

MongoNetworkError: failed to connect to server [localhost:27017] on first connect
[Error: connect ECONNREFUSED 127.0.0.1:27017 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)

Check if mongodb server is running in your local at port 27017.

(or)

Change the host and port details accordingly if it's running in other remote machine. For your reference.

Upvotes: 0

Related Questions