UnhandledPromiseRejectionWarning:MongoNetworkError:无法连接到服务器

时间:2020-07-24 17:42:55

标签: reactjs mongodb

我正在尝试在终端中运行一个node seed.js文件来连接我的项目mongodb后端服务器,但是始终收到以下错误:

    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.

我已经尝试通过在白名单中添加“ 0.0.0.0/0”来将我的IP地址列入白名单;它没有用。我还能做什么?

这是seed.js文件:

const {类型} = require(“ ./ models / genre”); const {电影} = require(“ ../ models / movie”); const mongoose = require(“ mongoose”); const config = require(“ config”);

const数据= [{ 名称:“喜剧”, 电影: [ {title:“ Airplane”,numberInStock:5,每日RentalRate:2}, {title:“ The Hangover”,numberInStock:10,dailyRentalRate:2}, {标题:“婚礼失败者”,数量库存:15,每日租金:2} ]},{ 名称:“动作”, 电影: [ {title:“ Die Hard”,numberInStock:5,每日RatealRate:2}, {title:“ Terminator”,numberInStock:10,dailyRentalRate:2}, {标题:“复仇者联盟”,库存数量:15,每日租金价格:2} ]},{ 名称:“浪漫”, 电影: [ {title:“ The Notebook”,numberInStock:5,每日RentalRate:2}, {标题:“当哈利遇见萨利”,numberInStock:10,每日RentalRate:2}, {标题:“ Pretty Woman”,numberInStock:15,每日RentalRate:2} ]},{ 名称:“惊悚片”, 电影: [ {标题:“第六感”,库存数量:5,每日租金价格:2}, {标题:“ Gone Girl”,numberInStock:10,每日RentalRate:2}, {title:“ The Others”,numberInStock:15,dailyRentalRate:2} ]}];

异步函数seed(){等待mongoose.connect(config.get(“ db”));

等待Movie.deleteMany({});等待Genre.deleteMany({});

用于(让数据流派){ const {_id:genreId} =等待新的Genre({name:genre.name})。save(); const movie = genre.movi​​es.map(movie =>({ ...电影, 体裁:{_id:genreId,名称:genre.name} })); 等待Movie.insertMany(movies); }

mongoose.disconnect();

console.info(“完成!”); }

seed();

1 个答案:

答案 0 :(得分:0)

错误似乎很容易解释

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)

检查mongodb服务器是否在local的端口27017上运行。

(或)

如果hostport在其他远程计算机上运行,请相应地进行更改。对于your reference