mongodb连接报错

来源:13-13 nodejs连接mongodb

神话2005

2023-04-24

const { MongoClient } = require(‘mongodb’)

const url = 'mongodb://localhost:27017’
const client = new MongoClient(url)

const dbName = ‘myblog’

async function main() {
// Use connect method to connect to the server
await client.connect();
console.log(‘Connected successfully to server’);
const db = client.db(dbName);
const collection = db.collection(‘documents’);

// the following code examples can be pasted here…

return ‘done.’;
}

main()
.then((res) => {
console.log(res,‘res------successfully’)
})
.catch((err) => {
console.error(err, ‘console.error--------------------’)
})
.finally(() => client.close());

连接报错

报错信息
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (E:\myCode\nodejs-myblog\code-hu\mongodb-test\node_modules\mongodb\lib\sdam\topology.js:277:38)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
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,
[Symbol(errorLabels)]: Set(0) {}
} console.error--------------------

写回答

1回答

双越

2023-04-24

你使用命令行连接呢?能成功吗

0
3
双越
回复
神话2005
先这样用着,继续学习
2023-04-25
共3条回复

Node.js+Express+Koa2+Nest.js 开发服务端

从入门到实战,一站式掌握 Node.js+Express+Koa2

4051 学习 · 2006 问题

查看课程