首次连接时无法连接到服务器 [localhost:27017] [错误:连接 ECONNREFUSED ---- 在 TCPConnectWrap.afterConnect [as oncomplete]

时间:2021-04-20 04:19:39

标签: node.js mongodb

我试图让 mongo db 存储数字 1 到 9999 拳头它会存储 0 到 1 然后是 0 到 2 等等....

for(var i = 0; i <= 9999; ++i) {
    console.log(i);
    a += i + " ";
    add(a, i);
}

并且我向对象添加了另一个变量以显示最后一个数字 当我运行此代码时:

var a = [];
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";
const dbName = 'numbzzz';

function add(item, lastnum) {
    return new Promise(async (resolve, reject) => {
        const client = new MongoClient(url);
        try {
            await client.connect();
            const db = client.db(dbName);
            const addedItem = await db.collection('nunb').insertOne({num: item, LNUM: lastnum});

            resolve(addedItem.ops[0]);
            client.close();
        } catch (error) {
            reject(error);
        }
    });
}

for(var i = 0; i <= 9999; ++i) {
    console.log(i);
    a += i + " ";
    add(a, i);
}

我在日志中得到了这个:

1
2
3
...
9999
(node:40664) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(Use `node --trace-warnings ...` to show where the warning was created)
D:\codde\mongoDB test\node_modules\mongodb\lib\core\topologies\server.js:438
          new MongoNetworkError(
          ^

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1137:16) {
  name: 'MongoNetworkError'
}]
    at Pool.<anonymous> (D:\codde\mongoDB test\node_modules\mongodb\lib\core\topologies\server.js:438:11)
    at Pool.emit (node:events:378:20)
    at D:\codde\mongoDB test\node_modules\mongodb\lib\core\connection\pool.js:562:14
    at D:\codde\mongoDB test\node_modules\mongodb\lib\core\connection\pool.js:995:11
    at D:\codde\mongoDB test\node_modules\mongodb\lib\core\connection\connect.js:32:7
    at callback (D:\codde\mongoDB test\node_modules\mongodb\lib\core\connection\connect.js:280:5)
    at Socket.<anonymous> (D:\codde\mongoDB test\node_modules\mongodb\lib\core\connection\connect.js:310:7)
    at Object.onceWrapper (node:events:485:26)
    at Socket.emit (node:events:378:20)
    at emitErrorNT (node:internal/streams/destroy:188:8)

所以我基本上得到了错误:

D:\codde\mongoDB test\node_modules\mongodb\lib\core\topologies\server.js:438
          new MongoNetworkError(
          ^

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1137:16) {
  name: 'MongoNetworkError'
}]
    at Pool.<anonymous> (D:\codde\mongoDB test\node_modules\mongodb\lib\core\topologies\server.js:438:11)
    at Pool.emit (node:events:378:20)
    at D:\codde\mongoDB test\node_modules\mongodb\lib\core\connection\pool.js:562:14
    at D:\codde\mongoDB test\node_modules\mongodb\lib\core\connection\pool.js:995:11
    at D:\codde\mongoDB test\node_modules\mongodb\lib\core\connection\connect.js:32:7
    at callback (D:\codde\mongoDB test\node_modules\mongodb\lib\core\connection\connect.js:280:5)
    at Socket.<anonymous> (D:\codde\mongoDB test\node_modules\mongodb\lib\core\connection\connect.js:310:7)
    at Object.onceWrapper (node:events:485:26)
    at Socket.emit (node:events:378:20)
    at emitErrorNT (node:internal/streams/destroy:188:8)

有人可以帮我解决问题吗?谢谢:)

0 个答案:

没有答案