I am working on a Node.js application that utilizes Mongoose as the database. Whenever I try to call any function from the MongoDB driver, such as find(), updateOne(), or deleteMany(), I am getting a massive error output in the terminal, but it does not provide any specific error message or useful information about the cause of the issue.
The error output appears to be a large object containing various MongoDB connection details, configurations, and function references. However, there is no clear indication of what is causing the error or how to resolve it.
Here's an example of the error output I'm receiving:
code:
const allForms = async (req, res) => {
try {
let data = await formModal.find();
if (!data) {
return res.status(400).send("Data is not found");
} else {
return res.send({ data: data });
}
} catch (error) {
console.error(error.message);
return res.status(500).json({ error: 'Internal server error' });
}
}
model code:
const mongoose = require('mongoose');
const formSchema = mongoose.Schema({
destination: {
type: String,
required: true
},
className: {
type: String,
enum: ['First', 'Second'],
required: true
},
duration: {
type: String,
enum: ['Monthly', 'Quarterly'],
required: true
},
line: {
type: String,
enum: ['Harbour', 'Western', 'Central'],
required: true
},
student: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Student'
}
});
const Form = mongoose.model('form', formSchema);
module.exports = Form;
output:
},
host: 'localhost',
port: 27017,
name: 'arcs'
},
queue: [],
buffer: false,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
},
'$__collection': <ref *6> Collection {
collection: Collection {
s: {
db: Db { s: [Object], client: [MongoClient] },
options: {
raw: false,
useBigInt64: false,
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
ignoreUndefined: false,
bsonRegExp: false,
serializeFunctions: false,
fieldsAsRaw: {},
enableUtf8Validation: true,
readPreference: [ReadPreference]
},
namespace: MongoDBCollectionNamespace {
db: 'arcs',
collection: 'students'
},
pkFactory: { createPk: [Function: createPk] },
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined,
maxStalenessSeconds: undefined,
minWireVersion: undefined
},
bsonOptions: {
raw: false,
useBigInt64: false,
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
ignoreUndefined: false,
bsonRegExp: false,
serializeFunctions: false,
fieldsAsRaw: {},
enableUtf8Validation: true
},
readConcern: undefined,
writeConcern: undefined
me: 'student',
_closed: false,
opts: {
autoIndex: true,
autoCreate: true,
autoSearchIndex: false,
schemaUserProvidedOptions: {},
capped: false,
Promise: undefined,
'$wasForceClosed': undefined
},
name: 'students',
collectionName: 'students',
conn: <ref *3> NativeConnection {
base: <ref *1> Mongoose {
connections: [ [Circular *3] ],
nextConnectionId: 1,
models: <ref *4> {
student: [Circular *2],
form: [Function],
admin: [Function]
},
events: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
},
__driver: {
Collection: [Function: NativeCollection],
Connection: [Function]
},
options: {
pluralization: true,
autoIndex: true,
autoCreate: true,
autoSearchIndex: false,
[Symbol(mongoose:default)]: true
},
_pluralize: [Function: pluralize],
Schema: [Function: Schema] {
reserved: [Object: null prototype],
Types: [Object],
ObjectId: [Function]
},
model: [Function (anonymous)],
plugins: [ [Array], [Array], [Array], [Array] ],
default: [Circular *1],
mongoose: [Circular *1],
cast: [Function: cast],
STATES: [Object: null prototype] {
'0': 'disconnected',
'1': 'connected',
'2': 'connecting',
'3': 'disconnecting',
'99': 'uninitialized',
disconnected: 0,
connected: 1,
connecting: 2,
disconnecting: 3,
uninitialized: 99
},
setDriver: [Function: setDriver],
set: [Function (anonymous)],
get: [Function (anonymous)],
createConnection: [Function (anonymous)],
connect: [AsyncFunction: connect],
disconnect: [AsyncFunction: disconnect],
startSession: [Function (anonymous)],
pluralize: [Function (anonymous)],
deleteModel: [Function (anonymous)],
modelNames: [Function (anonymous)],
plugin: [Function (anonymous)],
version: '8.2.4',
Mongoose: [Function: Mongoose],
SchemaType: [Function: SchemaType] {
cast: [Function: cast],
set: [Function: set],
get: [Function (anonymous)],
_isRef: [Function (anonymous)],
checkRequired: [Function (anonymous)],
CastError: [class CastError extends MongooseError],
ValidatorError: [class ValidatorError extends MongooseError]
},
SchemaTypes: {
Array: [Function],
BigInt: [Function],
Boolean: [Function],
Buffer: [Function],
Date: [Function],
Decimal: [Function],
Decimal128: [Function],
DocumentArray: [Function],
Map: [Function],
Mixed: [Function],
Number: [Function],
ObjectId: [Function],
String: [Function],
Subdocument: [Function],
UUID: [Function],
Oid: [Function],
Object: [Function],
Bool: [Function],
ObjectID: [Function]
},
VirtualType: [Function: VirtualType],
Types: {
Array: [Function: MongooseArray],
Buffer: [Function],
Embedded: [Function],
Document: [Function],
DocumentArray: [Function: MongooseDocumentArray],
Decimal128: [class Decimal128 extends BSONValue],
ObjectId: [Function],
Map: [class MongooseMap extends Map],
Subdocument: [Function: Subdocument],
UUID: [class UUID extends Binary]
},
Query: [Function: Query] { base: [Object], 'use$geoWithin': true },
Model: [Function: Model] {
exists: [Function: exists],
discriminator: [Function (anonymous)],
_events: undefined,
_eventsCount: 0,
_maxListeners: undefined,
setMaxListeners: [Function: setMaxListeners],
getMaxListeners: [Function: getMaxListeners],
emit: [Function: emit],
addListener: [Function: addListener],
on: [Function: addListener],
prependListener: [Function: prependListener],
once: [Function: once],
prependOnceListener: [Function: prependOnceListener],
removeListener: [Function: removeListener],
off: [Function: removeListener],
removeAllListeners: [Function: removeAllListeners],
listeners: [Function: listeners],
rawListeners: [Function: rawListeners],
listenerCount: [Function: listenerCount],
eventNames: [Function: eventNames],
init: [Function: init],
createCollection: [AsyncFunction: createCollection],
syncIndexes: [AsyncFunction: syncIndexes],
createSearchIndex: [AsyncFunction: createSearchIndex],
updateSearchIndex: [AsyncFunction: updateSearchIndex],
dropSearchIndex: [AsyncFunction: dropSearchIndex],
diffIndexes: [AsyncFunction: diffIndexes],
cleanIndexes: [AsyncFunction: cleanIndexes],
listIndexes: [AsyncFunction: listIndexes],
ensureIndexes: [AsyncFunction: ensureIndexes],
createIndexes: [AsyncFunction: createIndexes],
translateAliases: [Function: translateAliases],
deleteOne: [Function: deleteOne],
deleteMany: [Function: deleteMany],
find: [Function: find],
findById: [Function: findById],
findOne: [Function: findOne],
estimatedDocumentCount: [Function: estimatedDocumentCount],
countDocuments: [Function: countDocuments],
distinct: [Function: distinct],
where: [Function: where],
'$where': [Function: $where],
findOneAndUpdate: [Function (anonymous)],
findByIdAndUpdate: [Function (anonymous)],
findOneAndDelete: [Function (anonymous)],
findByIdAndDelete: [Function (anonymous)],
findOneAndReplace: [Function (anonymous)],
create: [AsyncFunction: create],
watch: [Function (anonymous)],
startSession: [Function (anonymous)],
insertMany: [AsyncFunction: insertMany],
'$__insertMany': [Function (anonymous)],
bulkWrite: [AsyncFunction: bulkWrite],
bulkSave: [AsyncFunction: bulkSave],
applyDefaults: [Function: applyDefaults],
castObject: [Function: castObject],
buildBulkWriteOperations: [Function: buildBulkWriteOperations],
hydrate: [Function (anonymous)],
updateMany: [Function: updateMany],
updateOne: [Function: updateOne],
replaceOne: [Function: replaceOne],
aggregate: [Function: aggregate],
validate: [AsyncFunction: validate],
populate: [AsyncFunction: populate],
compile: [Function: compile],
__subclass: [Function: subclass],
recompileSchema: [Function: recompileSchema],
inspect: [Function (anonymous)],
_applyQueryMiddleware: [Function: _applyQueryMiddleware],
[Symbol(nodejs.util.inspect.custom)]: [Function (anonymous)]
},
Document: [Function: Document] {
_events: undefined,
_eventsCount: 0,
_maxListeners: undefined,
setMaxListeners: [Function: setMaxListeners],
getMaxListeners: [Function: getMaxListeners],
emit: [Function: emit],
addListener: [Function: addListener],
on: [Function: addListener],
prependListener: [Function: prependListener],
once: [Function: once],
prependOnceListener: [Function: prependOnceListener],
removeListener: [Function: removeListener],
off: [Function: removeListener],
removeAllListeners: [Function: removeAllListeners],
listeners: [Function: listeners],
rawListeners: [Function: rawListeners],
listenerCount: [Function: listenerCount],
eventNames: [Function: eventNames],
ValidationError: [class ValidationError extends MongooseError]
},
ObjectId: [Function: SchemaObjectId] {
schemaName: 'ObjectId',
defaultOptions: {},
get: [Function (anonymous)],
set: [Function: set],
setters: [],
_checkRequired: [Function (anonymous)],
_cast: [Function: castObjectId],
cast: [Function: cast],
_defaultCaster: [Function (anonymous)],
checkRequired: [Function (anonymous)]
},
isValidObjectId: [Function (anonymous)],
isObjectIdOrHexString: [Function (anonymous)],
syncIndexes: [Function (anonymous)],
Decimal128: [Function: SchemaDecimal128] {
schemaName: 'Decimal128',
defaultOptions: {},
_cast: [Function: castDecimal128],
set: [Function: set],
setters: [],
get: [Function (anonymous)],
cast: [Function: cast],
_defaultCaster: [Function (anonymous)],
_checkRequired: [Function (anonymous)],
checkRequired: [Function (anonymous)]
},
Mixed: [Function: SchemaMixed] {
schemaName: 'Mixed',
UUID: [Getter],
MongoBulkWriteError: [Getter],
ClientEncryption: [Getter],
ChangeStreamCursor: [Getter],
MongoAPIError: [Getter],
MongoAWSError: [Getter],
MongoAzureError: [Getter],
MongoBatchReExecutionError: [Getter],
MongoChangeStreamError: [Getter],
MongoCompatibilityError: [Getter],
MongoCursorExhaustedError: [Getter],
},
mquery: [Function: Query] {
permissions: [Object],
_isPermitted: [Function (anonymous)],
canMerge: [Function (anonymous)],
setGlobalTraceFunction: [Function (anonymous)],
utils: [Object],
env: [Object],
Collection: [class NodeCollection extends Collection],
BaseCollection: [Function]
},
sanitizeFilter: [Function: sanitizeFilter],
trusted: [Function: trusted],
skipMiddlewareFunction: [Function: skipWrappedFunction],
overwriteMiddlewareResult: [Function: overwriteResult]
},
collections: {
students: [Circular *6],
forms: Collection {
collection: [Collection],
Promise: [Function: Promise],
modelName: 'form',
_closed: false,
opts: [Object],
name: 'forms',
collectionName: 'forms',
conn: [Circular *3],
queue: [],
buffer: false,
emitter: [EventEmitter]
},
admins: Collection {
collection: [Collection],
Promise: [Function: Promise],
modelName: 'admin',
_closed: false,
opts: [Object],
name: 'admins',
collectionName: 'admins',
conn: [Circular *3],
queue: [],
buffer: false,
emitter: [EventEmitter]
}
},
models: <ref *4> {
student: [Circular *2],
form: [Function: model] {
hooks: [Kareem],
base: [Mongoose],
modelName: 'form',
model: [Function: model],
db: [Circular *3],
discriminators: undefined,
events: [EventEmitter],
'$appliedMethods': true,
'$appliedHooks': true,
_middleware: [Kareem],
'$__insertMany': [Function (anonymous)],
schema: [Schema],
collection: [Collection],
'$__collection': [Collection],
Query: [Function],
'$init': [Promise],
'$caught': true,
[Symbol(mongoose#Model)]: true
},
admin: [Function: model] {
hooks: [Kareem],
base: [Mongoose],
modelName: 'admin',
model: [Function: model],
db: [Circular *3],
discriminators: undefined,
events: [EventEmitter],
'$appliedMethods': true,
'$appliedHooks': true,
_middleware: [Kareem],
'$__insertMany': [Function (anonymous)],
schema: [Schema],
collection: [Collection],
'$__collection': [Collection],
Query: [Function],
'$init': [Promise],
'$caught': true,
[Symbol(mongoose#Model)]: true
}
},
config: {},
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype] {
'0': 'disconnected',
'1': 'connected',
'2': 'connecting',
'3': 'disconnecting',
'99': 'uninitialized',
disconnected: 0,
connected: 1,
connecting: 2,
disconnecting: 3,
uninitialized: 99
},
_readyState: 1,
_closeCalled: undefined,
_hasOpened: true,
plugins: [],
id: 0,
_queue: [],
_listening: false,
_connectionOptions: { driverInfo: { name: 'Mongoose', version: '8.2.4' } },
_connectionString: 'mongodb://localhost:27017/arcs',
client: <ref *5> MongoClient {
_events: [Object: null prototype] {
serverDescriptionChanged: [Function (anonymous)]
},
_eventsCount: 1,
_maxListeners: 0,
mongoLogger: MongoLogger {
error: [Function: bound log],
warn: [Function: bound log],
info: [Function: bound log],
debug: [Function: bound log],
trace: [Function: bound log],
componentSeverities: [Object],
maxDocumentLength: 1000,
logDestination: [Object]
},
s: {
url: 'mongodb://localhost:27017/arcs',
bsonOptions: [Object],
namespace: [MongoDBNamespace],
hasBeenClosed: false,
sessionPool: [ServerSessionPool],
activeSessions: Set(0) {},
options: [Getter],
readConcern: [Getter],
writeConcern: [Getter],
readPreference: [Getter],
isMongoClient: [Getter]
},
topology: Topology {
_events: [Object: null prototype],
_eventsCount: 26,
_maxListeners: undefined,
client: [Circular *5],
selectServerAsync: [Function (anonymous)],
s: [Object],
[Symbol(kCapture)]: false,
[Symbol(waitQueue)]: [List]
},
connectionLock: undefined,
[Symbol(kCapture)]: false,
[Symbol(options)]: [Object: null prototype] {
hosts: [Array],
compressors: [Array],
connectTimeoutMS: 30000,
dbName: 'arcs',
directConnection: false,
driverInfo: [Object],
enableUtf8Validation: true,
forceServerObjectId: false,
heartbeatFrequencyMS: 10000,
loadBalanced: false,
localThresholdMS: 15,
maxConnecting: 2,
maxIdleTimeMS: 0,
maxPoolSize: 100,
minPoolSize: 0,
minHeartbeatFrequencyMS: 500,
monitorCommands: false,
noDelay: true,
pkFactory: [Object],
raw: false,
readPreference: [ReadPreference],
retryReads: true,
retryWrites: true,
serverMonitoringMode: 'auto',
serverSelectionTimeoutMS: 30000,
socketTimeoutMS: 0,
srvMaxHosts: 0,
srvServiceName: 'mongodb',
waitQueueTimeoutMS: 0,
zlibCompressionLevel: 0,
userSpecifiedAuthSource: false,
userSpecifiedReplicaSet: false,
mongoLoggerOptions: [Object],
metadata: [Object],
[Symbol(@@mdb.enableMongoLogger)]: false
}
},
'$initialConnection': Promise { [Circular *3] },
db: Db {
s: {
options: [Object],
readPreference: [ReadPreference],
bsonOptions: [Object],
pkFactory: [Object],
readConcern: undefined,
writeConcern: undefined,
namespace: [MongoDBNamespace]
},
client: <ref *5> MongoClient {
_events: [Object: null prototype],
_eventsCount: 1,
_maxListeners: 0,
mongoLogger: [MongoLogger],
s: [Object],
topology: [Topology],
connectionLock: undefined,
[Symbol(kCapture)]: false,
[Symbol(options)]: [Object: null prototype]
}
},
host: 'localhost',
port: 27017,
name: 'arcs'
},
queue: [],
buffer: false,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
},
Query: [Function (anonymous)] {
base: {
toConstructor: [Function: toConstructor],
setOptions: [Function (anonymous)],
collection: [Function: collection],
collation: [Function (anonymous)],
'$where': [Function (anonymous)],
where: [Function (anonymous)],
equals: [Function: equals],
eq: [Function: eq],
or: [Function: or],
nor: [Function: nor],
and: [Function: and],
gt: [Function (anonymous)],
gte: [Function (anonymous)],
lt: [Function (anonymous)],
lte: [Function (anonymous)],
ne: [Function (anonymous)],
in: [Function (anonymous)],
nin: [Function (anonymous)],
all: [Function (anonymous)],
regex: [Function (anonymous)],
size: [Function (anonymous)],
maxDistance: [Function (anonymous)],
minDistance: [Function (anonymous)],
mod: [Function (anonymous)],
exists: [Function (anonymous)],
elemMatch: [Function (anonymous)],
within: [Function: within],
box: [Function (anonymous)],
polygon: [Function (anonymous)],
circle: [Function (anonymous)],
near: [Function: near],
intersects: [Function: intersects],
geometry: [Function: geometry],
select: [Function: select],
slice: [Function (anonymous)],
sort: [Function (anonymous)],
limit: [Function (anonymous)],
skip: [Function (anonymous)],
batchSize: [Function (anonymous)],
comment: [Function (anonymous)],
maxTimeMS: [Function (anonymous)],
maxTime: [Function (anonymous)],
hint: [Function (anonymous)],
j: [Function: j],
slaveOk: [Function (anonymous)],
setReadPreference: [Function (anonymous)],
read: [Function (anonymous)],
r: [Function (anonymous)],
readConcern: [Function (anonymous)],
tailable: [Function (anonymous)],
w: [Function: writeConcern],
writeConcern: [Function: writeConcern],
wTimeout: [Function: wtimeout],
wtimeout: [Function: wtimeout],
merge: [Function (anonymous)],
find: [Function (anonymous)],
_find: [AsyncFunction: _find],
cursor: [Function (anonymous)],
findOne: [Function (anonymous)],
_findOne: [AsyncFunction: _findOne],
count: [Function (anonymous)],
_count: [AsyncFunction: _count],
distinct: [Function (anonymous)],
_distinct: [AsyncFunction: _distinct],
updateMany: [Function: updateMany],
_updateMany: [AsyncFunction (anonymous)],
updateOne: [Function: updateOne],
_updateOne: [AsyncFunction (anonymous)],
replaceOne: [Function: replaceOne],
_replaceOne: [AsyncFunction (anonymous)],
deleteOne: [Function (anonymous)],
_deleteOne: [AsyncFunction (anonymous)],
deleteMany: [Function (anonymous)],
_deleteMany: [AsyncFunction (anonymous)],
findOneAndUpdate: [Function (anonymous)],
_findOneAndUpdate: [AsyncFunction (anonymous)],
findOneAndDelete: [Function (anonymous)],
findOneAndRemove: [Function (anonymous)],
_findOneAndRemove: [AsyncFunction (anonymous)],
setTraceFunction: [Function (anonymous)],
exec: [AsyncFunction: exec],
then: [AsyncFunction (anonymous)],
selected: [Function: selected],
selectedInclusively: [Function: selectedInclusively],
selectedExclusively: [Function: selectedExclusively],
_mergeUpdate: [Function (anonymous)],
_optionsForExec: [Function (anonymous)],
_fieldsForExec: [Function (anonymous)],
_updateForExec: [Function (anonymous)],
_ensurePath: [Function (anonymous)],
_validate: [Function (anonymous)]
}
},
'$init': Promise { undefined, catch: [Function (anonymous)] },
'$caught': true,
[Symbol(mongoose#Model)]: true
}
}
Node.js v20.10.0
[nodemon] app crashed - waiting for file changes before starting...
I have tried various troubleshooting steps, such as reinstalling the MongoDB driver, updating Node.js, and checking my connection string, but none of these have resolved the issue.
When calling Mongoose functions, I expect to either receive the desired result or a clear and descriptive error message that can help me identify and resolve the issue.
Can anyone guide me on how to resolve this problem and get meaningful error messages when calling MongoDB functions? Any help would be greatly appreciated.