Home > OS >  Model.findOne() is returning null
Model.findOne() is returning null

Time:10-09

Model.findOne() is returning null even if the valid collection is present in the respective Model


app.post("/fleetManagement", (req, res) => {

  const requestedDriverID = req.body.driverId;
  console.log(requestedDriverID);

  Driver.findOne({
      _id: requestedDriverID
  }, function(err, requestedDriverResult) {
      console.log(requestedDriverResult);
      res.render("fleetManagement", {
          reqDriver: requestedDriverResult
      });
  });
})

Output Collection in Driver Model

Check out the Output and Collection of Driver Model

CodePudding user response:

Try convert requestedDriverID to ObjectId(requestedDriverID)

CodePudding user response:

You need to convert the _id input to ObjectId. Here's the updated code for your reference:


app.post("/fleetManagement",(req, res)=>{

const requestedDriverID = req.body.driverId;
console.log( requestedDriverID);

Driver.findOne({_id: ObjectId(requestedDriverID)  }, function(err, requestedDriverResult){
console.log(requestedDriverResult);
  res.render("fleetManagement", {
    reqDriver:requestedDriverResult
    });
  });
})
  • Related