I am using a sample MongoDB Database for a project, while using the findById mongoose method, I get the error: 'CastError: Cast to ObjectId failed for value "..." at path "_id" for model "Company"'. I am using handlebars as the view engine.
Middleware file:
getCompanyData: async (req, res, next) => {
// Queries
const information =
"name ipo founded_day founded_month founded_year description overview relationships";
// try {
const getCompanyData = await Companies.findById(req.params.id, information).exec();
console.log(getCompanyData);
// Coverting Mongoose Document to Object
const companyData = getCompanyData.toObject()
// console.log(companyData);
Here is the routes file
// Company Route
router.get('/list/:id', getCompanyData, (req, res) => {
// console.log(req.companyData.name);
res.render('company', {
comapany: req.companyData
})
})
information
in findById(),I dont think you can do such way. what are you trying to achieve btw? – Karl L