the db connection is like this with ObjectId()
const mongodb = require('mongodb');
const mongoClient = mongodb.MongoClient;
const ObjectId = mongodb.ObjectId;
const url = 'mongodb://127.0.0.1:27017/';
let _db ;
const mongodbConnection = ()=>{
mongoClient
.connect(url, { useNewUrlParser: true })
.then((client) => {
_db = client.db('crud');
console.log(`mongo db is connected and the database is ${_db}`);
})
.catch((error) => {
throw console.error(err)
});
}
const getDb= ()=>{
return _db;
}
const _ID = (id)=>{
return new ObjectId(id);
}
module.exports={mongodbConnection,getDb,_ID};
this for the update route from form
route.post('/edit', (req,res)=>{
const fullname = req.body.fullname;
const phone = req.body.phone;
const id = JSON.stringify(req.body._id); ;
mongodb()
.collection("Regiter")
.updateOne(
{ _id: _ID(id)},
{ $set: { Fullname: fullname, PhoneNumber: phone } }
)
.then(result => {
res.redirect("/display");
console.log(result.modifiedCount);
})
.catch(err => {
console.log(err);
});
});
the problem is when converting req.body._id to ObjectId to update