无法与 Mongoose (Mongodb) 连接
Posted
技术标签:
【中文标题】无法与 Mongoose (Mongodb) 连接【英文标题】:Unable to connect with Mongoose (Mongodb) 【发布时间】:2020-06-07 05:42:33 【问题描述】:const express=require('express');
const mongoose=require('mongoose');
const morgan=require('morgan');
const path=require('path');
const app = express();
const PORT = process.env.PORT || 8080;
mongoose.connect('mongodb://localhost/SUCCESS',
useNewUrlParser: true,
useUnifiedTopology: true
);
mongoose.connection.on('connected', () =>
console.log('Mongoose is connected');
)
//HTTP request logger
app.use(morgan('tiny'));
//Routes
app.get('/', (req, res) =>
const data =
username: 'caa',
age: 5
;
res.json(data);
);
app.get('/api/name', (req, res) =>
const data =
username: 'caa',
age: 5
;
res.json(data);
);
app.listen(PORT, console.log(`Server is starting at $PORT`));
node pages/server.js
Server is starting at 8080
(node:6253) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at new MongooseServerSelectionError (/Users/Abc/Desktop/success/node_modules/mongoose/lib/error/serverSelection.js:22:11)
at NativeConnection.Connection.openUri (/Users/Abc/Desktop/success/node_modules/mongoose/lib/connection.js:808:32)
at Mongoose.connect (/Users/Abc/Desktop/success/node_modules/mongoose/lib/index.js:333:15)
at Object.<anonymous> (/Users/Abc/Desktop/success/pages/server.js:18:10)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
(node:6253) 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:6253) [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.
【问题讨论】:
是网络或服务相关问题,可能是MongoDB服务造成的,请检查是否在运行。如果它仍然存在,请指定正在运行的操作系统以获取更多详细信息。 如何检查? @KamNadimi 你运行的操作系统是什么? MACOS @KamNadimi 在你的终端试试这个mongod
【参考方案1】:
我知道我迟到了,但这应该可以解决它。
const db = mongoose.connect("mongodb://localhost/swag-shop",
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
).then(() =>
console.log("Connected To Mongo Db DataBase");
).catch((err) =>
console.log("DataBase Connection Error " + err);
);
然后在 windows 中的 powershell 或 mac 的终端中输入“Mongod”,然后在终端中创建另一个选项卡或打开另一个 powershell 窗口,然后输入“Mongo”,这应该可以解决您的问题。
【讨论】:
【参考方案2】:我有同样的问题。请像这样更新您的代码。
mongoose.connect("mongodb://127.0.0.1:27017/SUCCESS",
useCreateIndex:true,
useNewUrlParser: true,
useUnifiedTopology: true).then(()=>
console.log('Database Successfully Connected'),error =>
console.log(error))
下一步,
-
转到您的任务管理器
服务
启动“MongoDB”
希望这会奏效。
【讨论】:
【参考方案3】://好像你没有定义mangoose的默认端口,请参考下面的代码
const mongoose = require('mongoose');
const db = "mongodb://localhost:27017/SUCCESS";
mongoose.connect(db,
useCreateIndex:true,
useUnifiedTopology:true,
useNewUrlParser:true
).then( () =>
console.log("Connected To Mongo Db DataBase");
).catch((err) =>
console.log("DataBase Connection Error " + err);
)
【讨论】:
我收到此错误:DataBase Connection Error MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017以上是关于无法与 Mongoose (Mongodb) 连接的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 nestjs/mongoose 连接 mongoDB
NestJs / Mongoose 在 Docker 撰写时无法建立 MongoDB 数据库连接
节点 js 错误:Mongoose 与 MongoDB 连接字符串
无法通过节点休息应用程序上的mongoose连接到mongodb-altas,连接失败:“UnhandledPromiseRejectionWarning:错误:连接EACCES”