This is my current code to find all entries from MongoDB atlas.
const { MongoClient } = require("mongodb");
const client = new MongoClient(url);
const dbName = "Chats";
const db = client.db(dbName);
const col = db.collection("Users");
app.post('/chat/enter', (req, res) => {
let newmsg = new DataClass(req.body.name, req.body.textChat);
add(newmsg).catch(console.dir);
res.render(path.join(__dirname + "/index.ejs"),{texts:texts});
})
async function add(x) {
try{
await client.connect();
await col.insertOne({"name":x.name,"textChat":x.msg});
}catch (e) {
console.log(e);
}
}
async function find(){
try{
await client.connect();
const a= await col.find({});
console.log(a);
}catch(e){
console.log(e);
}
}
find();
I want to display all the entries from my atlas database but so far I am getting this gibberish. This problem only arises when I use the find keyword or the output is more than one. findOne is working properly.
The output from the terminal is –
FindCursor {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false,
[Symbol(topology)]: Topology {
_events: [Object: null prototype] {
topologyDescriptionChanged: [Array],
connectionPoolCreated: [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)],
connectionPoolCleared: [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)],
error: [Function (anonymous)],
timeout: [Function (anonymous)],
close: [Function (anonymous)],
serverHeartbeatStarted: [Function (anonymous)],
serverHeartbeatSucceeded: [Function (anonymous)],
serverHeartbeatFailed: [Function (anonymous)]
},
_eventsCount: 25,
_maxListeners: undefined,
bson: [Object: null prototype] {
serialize: [Function: serialize],
deserialize: [Function: deserialize]
},
s: {
id: 0,
options: [Object: null prototype],
seedlist: [Array],
state: 'connected',
description: [TopologyDescription],
serverSelectionTimeoutMS: 30000,
heartbeatFrequencyMS: 10000,
minHeartbeatFrequencyMS: 500,
servers: [Map],
sessionPool: [ServerSessionPool],
sessions: Set(0) {},
credentials: [MongoCredentials],
clusterTime: [Object],
connectionTimers: Set(0) {},
detectShardedTopology: [Function: detectShardedTopology],
detectSrvRecords: [Function: detectSrvRecords],
srvPoller: [SrvPoller]
},
[Symbol(kCapture)]: false,
[Symbol(waitQueue)]: Denque {
_head: 3,
_tail: 3,
_capacity: undefined,
_capacityMask: 3,
_list: [Array]
}
},
[Symbol(namespace)]: MongoDBNamespace { db: 'Chats', collection: 'Users' },
[Symbol(documents)]: [],
[Symbol(initialized)]: false,
[Symbol(closed)]: false,
[Symbol(killed)]: false,
[Symbol(options)]: {
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined,
maxStalenessSeconds: undefined,
minWireVersion: undefined
},
fieldsAsRaw: {},
promoteValues: true,
promoteBuffers: false,
promoteLongs: true,
serializeFunctions: false,
ignoreUndefined: false,
bsonRegExp: false,
raw: false,
enableUtf8Validation: true
},
[Symbol(filter)]: {},
[Symbol(builtOptions)]: {
raw: false,
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
ignoreUndefined: false,
bsonRegExp: false,
serializeFunctions: false,
fieldsAsRaw: {},
enableUtf8Validation: true,
writeConcern: WriteConcern { w: 'majority' },
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined,
maxStalenessSeconds: undefined,
minWireVersion: undefined
}
}
}
2
Answers
Try to initialize the database connection in a separate method:
use toArray()