Node.js 无法在第一次连接时连接到服务器[localhost:27017]。[错误:在TCPConnectWrap.afterConnect[as oncomplete]处连接EconRefused---
我试图让mongo db存储数字1到9999,它将存储0到1,然后是0到2,以此类推Node.js 无法在第一次连接时连接到服务器[localhost:27017]。[错误:在TCPConnectWrap.afterConnect[as oncomplete]处连接EconRefused---,node.js,mongodb,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); } (变量i=0;i)的{ const client=新的MongoClient(url); 试一试{ 等待client.connect(); const db=client.db(dbName); const a
for(var i = 0; i <= 9999; ++i) {
console.log(i);
a += i + " ";
add(a, i);
}
(变量i=0;i)的{
const client=新的MongoClient(url);
试一试{
等待client.connect();
const db=client.db(dbName);
const addedItem=await db.collection('nunb').insertOne({num:item,LNUM:lastnum});
决议(增补条款[0]);
client.close();
}捕获(错误){
拒绝(错误);
}
});
}
对于(var i=0;我请帮我解决这个问题…econnreference表示连接被主动拒绝,因为请求的端口上没有进程侦听。
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)