Hi
I have the following code to add user to mongodb data base
in my server.js file i have following code
app.post("/AddUser",async (req,res)=>{
const credintials= await req.body
console.log(credintials)
const result=await AddUser(credintials)
and AddUser function has the following code
import { MongoClient } from "mongodb";
const uri =
"mongodb+srv://******@cluster0.xerwiw2.mongodb.net/?maxIdleTimeMS=5000";
const client = new MongoClient(uri);
async function AddUser(Credintials) {
try {
const res = await client
.db("Projectman")
.collection("Projectma")
.insertOne({ email: Credintials.email, pass: Credintials.Password })
.then((res) => {
return res;
})
.catch((err) => {
console.log(err);
});
if (res.acknowledged === true) {
const user = await client
.db("Projectman")
.collection("Projectma")
.findOne({ email: Credintials.email })
.then((res) => {
return res;
})
.catch((err) => {
return "no user found";
});
return user;
} else {
return "error";
}
} finally {
console.log("done");
}
}
export default AddUser;
I have noticed that the nodejs server is always connected to mongodb database even if no
client.connect() function is used
is this correct