这是将express js连接到mongodb的简单代码,我也安装了这两个包,那么为什么我会遇到这个错误呢?
const express = require("express");
const app = express();
app.listen(3000,() => {
console.log("Server is running at port number 3000");
})
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/myDatabase",{
useNewUrlParser:true,
useUnifiedTopology:true
})
.then(()=>{console.log("Connection is Succeed")})
.catch((e) => { console.error("Received an Error", e); });
字符串
我面临的错误,他们没有连接。
Server is running at port number 3000
(node:25888) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(Use `node --trace-warnings ...` to show where the warning was created)
(node:25888) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
Received an Error MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at _handleConnectionErrors (C:\Users\ASUS\OneDrive\Desktop\web dev\Backend Development\Express and MongoDB connections\node_modules\mongoose\lib\connection.js:809:11)
at NativeConnection.openUri (C:\Users\ASUS\OneDrive\Desktop\web dev\Backend Development\Express and MongoDB connections\node_modules\mongoose\lib\connection.js:784:11) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
型
2条答案
按热度按时间ds97pgxw1#
我遇到了同样的问题。只需将
localhost
替换为127.0.0.1
。因此您的代码将从以下更改:字符串
收件人:
型
kxxlusnw2#
请尝试以下代码,而不是使用'mongodb://localhost:27017/myDatabase'
字符串