mongoose Express JS和Mongo DB连接问题

ac1kyiln  于 9个月前  发布在  Go
关注(0)|答案(2)|浏览(125)

这是将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
}

ds97pgxw

ds97pgxw1#

我遇到了同样的问题。只需将localhost替换为127.0.0.1。因此您的代码将从以下更改:

mongoose.connect("mongodb://localhost:27017/myDatabase",{
    useNewUrlParser:true,
    useUnifiedTopology:true
})

字符串
收件人:

mongoose.connect("mongodb://127.0.0.1:27017/myDatabase",{
    useNewUrlParser:true,
    useUnifiedTopology:true
})

kxxlusnw

kxxlusnw2#

请尝试以下代码,而不是使用'mongodb://localhost:27017/myDatabase'

const express = require("express");
const mongoose = require("mongoose");
const app = express();

app.listen(3000,() => {
    console.log("Server is running at port number 3000");
})

mongoose.connect("mongodb://127.0.0.1:27017/myDatabase",{
    useNewUrlParser:true,
    useUnifiedTopology:true
})
.then(()=>{console.log("Connection is Succeed")})
.catch((e)=>{console.log("Received an Error")});

字符串

相关问题