提问人:Mayur Athavale 提问时间:12/16/2022 更新时间:12/16/2022 访问量:174
mongoDB服务器连接和基本操作代码不起作用
mongoDB server connection and basic operation code not working
问:
我设置了我的mongoDB数据库,然后添加了代码以连接到mongoDB并执行基本操作,例如将项目添加到数据库,更新它,然后将其从数据库中删除,最后关闭index.js文件中的客户端服务器。
但是当我尝试在保持mongoDB服务器打开的同时运行索引文件时,我收到一些我不知道的长错误消息......
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://127.0.0.1:27017/';
const dbname = 'conFusion';
MongoClient.connect(url)
.then((err, client) => {
assert.equal(err, null);
console.log('Connected correctly to Server');
const db = client.db(dbname);
const collection = db.collection('dishes');
collection.insertOne({"name":"Uthappizza", "description":"test"})
.then((err, res) => {
assert.equal(err, null);
console.log('After Insert:\n');
console.log(res.ops);
collection.find({}).toArray((err, docs) => {
assert.equal(err, null);
console.log('Found:\n');
console.log(docs)
db.collection.remove('dishes')
.then((err, res) => {
assert.equal(err, null);
client.close();
});
});
});
});
输出:
> [email protected] start
> node index
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
AssertionError [ERR_ASSERTION]: <ref *1> MongoClient {
[Symbol(kCapture)]: false,
[Symbol(options)]: [Object: null prototype] {
compressors: [
'none'
],
connectTimeoutMS: 30000,
dbName: 'test',
directConnection: false,
enableUtf8Validation: true,
forceServerObjectId: false,
heartbeatFrequencyMS: 10000,
hosts: [
HostAddress {
host: '127.0.0.1',
isIPv6: false,
port: 27017,
socketPath: undefined
}
],
keepAlive: true,
keepAliveInitialDelay... == null
at C:\Users\LENOVO\Desktop\mongoDB project\index.js:10:12
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: <ref *1> MongoClient {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
s: {
url: 'mongodb://127.0.0.1:27017/',
bsonOptions: {
raw: false,
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
ignoreUndefined: false,
bsonRegExp: false,
serializeFunctions: false,
fieldsAsRaw: {},
enableUtf8Validation: true
},
namespace: MongoDBNamespace { db: 'admin', collection: undefined },
hasBeenClosed: false,
sessionPool: ServerSessionPool {
client: [Circular *1],
sessions: List {
count: 0,
head: <ref *2> {
next: [Circular *2],
prev: [Circular *2],
value: null
}
}
},
activeSessions: Set(0) {},
options: [Getter],
readConcern: [Getter],
writeConcern: [Getter],
readPreference: [Getter],
logger: [Getter],
isMongoClient: [Getter]
},
topology: Topology {
_events: [Object: null prototype] {
connectionPoolCreated: [Function (anonymous)],
connectionPoolReady: [Function (anonymous)],
connectionPoolCleared: [Function (anonymous)],
connectionPoolClosed: [Function (anonymous)],
connectionCreated: [Function (anonymous)],
connectionReady: [Function (anonymous)],
connectionClosed: [Function (anonymous)],
connectionCheckOutStarted: [Function (anonymous)],
connectionCheckOutFailed: [Function (anonymous)],
connectionCheckedOut: [Function (anonymous)],
connectionCheckedIn: [Function (anonymous)],
commandStarted: [Function (anonymous)],
commandSucceeded: [Function (anonymous)],
commandFailed: [Function (anonymous)],
serverOpening: [Function (anonymous)],
serverClosed: [Function (anonymous)],
serverDescriptionChanged: [Function (anonymous)],
topologyOpening: [Function (anonymous)],
topologyClosed: [Function (anonymous)],
topologyDescriptionChanged: [Function (anonymous)],
error: [Function (anonymous)],
timeout: [Function (anonymous)],
close: [Function (anonymous)],
serverHeartbeatStarted: [Function (anonymous)],
serverHeartbeatSucceeded: [Function (anonymous)],
serverHeartbeatFailed: [Function (anonymous)]
},
_eventsCount: 26,
_maxListeners: undefined,
selectServerAsync: [Function (anonymous)],
bson: [Object: null prototype] {
serialize: [Function: serialize],
deserialize: [Function: deserialize]
},
s: {
id: 0,
options: [Object: null prototype] {
hosts: [ [HostAddress] ],
compressors: [ 'none' ],
connectTimeoutMS: 30000,
directConnection: false,
metadata: {
driver: [Object],
os: [Object],
platform: 'Node.js v18.12.1, LE (unified)|Node.js v18.12.1, LE (unified)'
},
enableUtf8Validation: true,
forceServerObjectId: false,
heartbeatFrequencyMS: 10000,
keepAlive: true,
keepAliveInitialDelay: 120000,
loadBalanced: false,
localThresholdMS: 15,
logger: Logger { className: 'MongoClient' },
maxConnecting: 2,
maxIdleTimeMS: 0,
maxPoolSize: 100,
minPoolSize: 0,
minHeartbeatFrequencyMS: 500,
monitorCommands: false,
noDelay: true,
pkFactory: { createPk: [Function: createPk] },
raw: false,
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined,
maxStalenessSeconds: undefined,
minWireVersion: undefined
},
retryReads: true,
retryWrites: true,
serverSelectionTimeoutMS: 30000,
socketTimeoutMS: 0,
srvMaxHosts: 0,
srvServiceName: 'mongodb',
waitQueueTimeoutMS: 0,
zlibCompressionLevel: 0,
dbName: 'test',
userSpecifiedAuthSource: false,
userSpecifiedReplicaSet: false
},
seedlist: [
HostAddress {
host: '127.0.0.1',
port: 27017,
socketPath: undefined,
isIPv6: false
}
],
state: 'connected',
description: TopologyDescription {
type: 'Single',
servers: Map(1) { '127.0.0.1:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: 30
},
serverSelectionTimeoutMS: 30000,
heartbeatFrequencyMS: 10000,
minHeartbeatFrequencyMS: 500,
servers: Map(1) {
'127.0.0.1:27017' => Server {
_events: [Object: null prototype],
_eventsCount: 18,
_maxListeners: undefined,
serverApi: undefined,
s: [Object],
[Symbol(kCapture)]: false,
[Symbol(monitor)]: [Monitor]
}
},
credentials: undefined,
clusterTime: undefined,
connectionTimers: Set(0) {},
detectShardedTopology: [Function: detectShardedTopology],
detectSrvRecords: [Function: detectSrvRecords]
},
client: [Circular *1],
[Symbol(kCapture)]: false,
[Symbol(waitQueue)]: List {
count: 0,
head: <ref *3> {
next: [Circular *3],
prev: [Circular *3],
value: null
}
}
},
[Symbol(kCapture)]: false,
[Symbol(options)]: [Object: null prototype] {
hosts: [
HostAddress {
host: '127.0.0.1',
port: 27017,
socketPath: undefined,
isIPv6: false
}
],
compressors: [ 'none' ],
connectTimeoutMS: 30000,
directConnection: false,
metadata: {
driver: { name: 'nodejs', version: '4.12.1' },
os: {
type: 'Windows_NT',
name: 'win32',
architecture: 'x64',
version: '10.0.22621'
},
platform: 'Node.js v18.12.1, LE (unified)|Node.js v18.12.1, LE (unified)'
},
enableUtf8Validation: true,
forceServerObjectId: false,
heartbeatFrequencyMS: 10000,
keepAlive: true,
keepAliveInitialDelay: 120000,
loadBalanced: false,
localThresholdMS: 15,
logger: Logger { className: 'MongoClient' },
maxConnecting: 2,
maxIdleTimeMS: 0,
Node.js v18.12.1
我试图摆脱所有已弃用的方法并再次运行索引文件,但相同的错误仍然存在。
答: 暂无答案
评论