I want to fetch the leaves count from leaves table by using sequelize but it gives me error of undefined (reading 'unscoped'). I am using postgresql and NodeJS.
here is my query
router.get('/:userId', checkToken, authorize(), async (req, res) => {
try {
const { params } = req;
const { userId } = params;
const leaveCounts = await db.leaves.unscoped().findAll({
attributes: [[db.Sequelize.literal('SELECT count(distinct(id)) from leaves'), 'leaveCount']],
where: {
"userId": {
userId
}
},
raw: true
});
const data = {
leaveCounts
}
res.status(200).json({
data
})
}
catch (e) {
console.log("error in status: ", e)
res.status(500).json({
error: "Internal Server Error",
status: false,
})
}
})
export default router;
error:
error in status: TypeError: Cannot read properties of undefined (reading 'unscoped')
Postman response:
{
"error": "Internal Server Error",
"status": false
}
please help how to do this.
CodePudding user response:
leaves property in db object may be undefined.
Check the type of db.leaves
ex) console.log(typeof db.leaves)
