首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不适用于MongoDB的NodeJS数据库操作

不适用于MongoDB的NodeJS数据库操作
EN

Stack Overflow用户
提问于 2022-11-20 01:07:04
回答 1查看 58关注 0票数 0

我试图在MongoDB中使用Node.js,但它不是使用最简单的代码。

最简单的代码:

代码语言:javascript
复制
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function (err, db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});

错误:

代码语言:javascript
复制
D:\Clg\Sem-5\Software Packages\Lab\Practical-7\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 (D:\Clg\Sem-5\Software Packages\Lab\Practical-7\node_modules\mongodb\lib\sdam\topology.js:292:38)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 2642697,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (D:\Clg\Sem-5\Software Packages\Lab\Practical-7\node_modules\mongodb\lib\cmap\connect.js:387:20)
            at Socket.<anonymous> (D:\Clg\Sem-5\Software Packages\Lab\Practical-7\node_modules\mongodb\lib\cmap\connect.js:310:22)
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          cause: Error: connect ECONNREFUSED ::1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) {
            errno: -4078,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '::1',
            port: 27017
          },
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

Node.js v18.7.0

我测试了最新版本和5.0.13版本的错误,但我也有相同的错误&当我在互联网上搜索时,我发现MongoDB服务应该有问题,但当我检查它时。我发现那个服务已经在运行了。

EN

回答 1

Stack Overflow用户

发布于 2022-11-20 01:33:33

ECONNREFUSED通常意味着MongoDB不在您提供的URL上运行,或者您的应用程序无法访问该端口。您肯定是在27017端口本地运行MongoDB吗?您需要确保本地MongoDB实例已启动并可用--尝试通过浏览器导航到localhost:27017来访问它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74504969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档