Ive taken all the async out of my code when connecting to mongo db
<code>`const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv:.......";
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
try
{
function run()
{
// Connect the client to the server (optional starting in v4.7)
let swi=false;
try
{
client.connect();//conneted to the server
swi=true;
}
catch(err)
{
swi=false;
}
if(swi==true)
{
client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
const db=client.db("Rehab");
const coll=db.collection("Support");
// Send a ping to confirm a successful connection
let front=false;
//let result=coll.insertOne({data:"Hello dudes"});
let find=coll.findOne({Email:"[email protected]"}).then;//The .then changes the console.log
console.log(find);
/*try
{
let find=coll.findOne({});
}
catch(err)
{
console.log(err);
}*/
}
else{console.log("Not working");}
}
run();
}
catch(err)
{
console.log(err)
}
//client.close();`
</code>
<code>`const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv:.......";
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
try
{
function run()
{
// Connect the client to the server (optional starting in v4.7)
let swi=false;
try
{
client.connect();//conneted to the server
swi=true;
}
catch(err)
{
swi=false;
}
if(swi==true)
{
client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
const db=client.db("Rehab");
const coll=db.collection("Support");
// Send a ping to confirm a successful connection
let front=false;
//let result=coll.insertOne({data:"Hello dudes"});
let find=coll.findOne({Email:"[email protected]"}).then;//The .then changes the console.log
console.log(find);
/*try
{
let find=coll.findOne({});
}
catch(err)
{
console.log(err);
}*/
}
else{console.log("Not working");}
}
run();
}
catch(err)
{
console.log(err)
}
//client.close();`
</code>
`const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv:.......";
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
try
{
function run()
{
// Connect the client to the server (optional starting in v4.7)
let swi=false;
try
{
client.connect();//conneted to the server
swi=true;
}
catch(err)
{
swi=false;
}
if(swi==true)
{
client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
const db=client.db("Rehab");
const coll=db.collection("Support");
// Send a ping to confirm a successful connection
let front=false;
//let result=coll.insertOne({data:"Hello dudes"});
let find=coll.findOne({Email:"[email protected]"}).then;//The .then changes the console.log
console.log(find);
/*try
{
let find=coll.findOne({});
}
catch(err)
{
console.log(err);
}*/
}
else{console.log("Not working");}
}
run();
}
catch(err)
{
console.log(err)
}
//client.close();`
After running this code on cmd using nodemon i get
Promise { }
After adding .then i get
[Function: then]
How would i fix the display of this what do i need to do inorder to convert the promise, without async.
Or could somone explain to me why i need to use async? Thank you
<code>`const { MongoClient, ServerApiVersion } = require('mongodb');`
</code>
<code>`const { MongoClient, ServerApiVersion } = require('mongodb');`
</code>
`const { MongoClient, ServerApiVersion } = require('mongodb');`
Also could someone explain the {} above and how require(‘mongodb’) is stored onto the const, just how it is stored and not why it is stored. Is it stored on MongoClient and ServerApiVersion?