我正在尝试将我的节点应用程序与mongodb连接。以前可以使用,但现在显示“第一次连接时无法连接到服务器[localhost:5000]。
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5000;
const mongoose = require('mongoose');
// connection to the db
mongoose.connect('mongodb://localhost:5000/dbname', { useNewUrlParser: true });
const DB = mongoose.connection;
DB.once('open', () => console.log("We are connected to the database"));
DB.on('error', () => console.log("Error connecting to the databse"));
连接到数据库时出错 (节点:11156)UnhandledPromiseRejectionWarning:MongoNetworkError:无法连接[[object Object]]
at Pool.<anonymous> (E:\projects\project\node_modules\mongodb-core\lib\topo
at Pool.emit (events.js:189:13)
at connect (E:\projects\project\node_modules\mongodb-core\lib\connection\po
at callback (E:\projects\project\node_modules\mongodb-core\lib\connection\c
at runCommand (E:\projects\project\node_modules\mongodb-core\lib\connection
at Connection.errorHandler (E:\projects\project\node_modules\mongodb-core\lrojects\project\node_modules\mongodb:277:13)-core\lib\connection\connect.js:321:9:13)5)
ts\project\node_modules\mongodb-core\lib\co
at Object.onceWrapper (events.js):277:13) :284:12)
at Conn
ection.emit(events.js:18dable.js:265:11)9:13)_visible.js:220:10)
at Socket.<anonymous> (E:\projec(internal/stream_base_commons.js:94:17)ts\project\node_modules\mongodb-coreonWarning: Unhandled promise rejection. Thi\lib\connection\connection.js:498:20tch block, or by rejecting a promise which )
at Socket.emit (events.js:189:13rning: Unhandled promise rejections are dep) the Node.js process with a non-zero exit c
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread]
(内部/stream_base_commons.js:94:17)
(节点:11156)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:1)(节点:11156)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。