当我尝试使用 Mongoose 将我的应用程序与数据库连接时遇到问题。已经尝试过以下我在谷歌上找到的解决方案:
在Windows上重新启动MongoDB服务,使用位于MongoDB的bin文件上的cmd手动打开数据库,但我无法解决它。谁能帮我?
这是我的index.js
const { MongoClient } = require('mongodb')
const url = 'mongodb://localhost:27017';
const database = 'e-comm'
const client = new MongoClient(url);
async function getData()
{
let result = await client.connect();
let db = result.db(database);
let collection = db.collection('products');
let response = await collection.find({}).toArray();
console.log(response);
}
getData();
Run Code Online (Sandbox Code Playgroud)
并抛出错误
C:\Users\HP\new node\node_modules\mongodb\lib\sdam\topology.js:292
const timeoutError = new error_1.MongoServerSelectionError(`Server selection
timed out after ${serverSelectionTimeoutMS} ms`, this.description);
^
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (C:\Users\HP\new node\node_modules\mongodb\lib\sdam\topology.js:29
2:38)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) …Run Code Online (Sandbox Code Playgroud)