NodsJs: TypeError: Cannot read properties of undefined (reading 'unscoped')
NodsJs: TypeError: Cannot read properties of undefined (reading 'unscoped')
我想通过使用 sequelize
从叶子 table 获取叶子数,但它给我错误未定义(读取 'unscoped')。我正在使用 postgresql 和 NodeJS。
这是我的查询
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 in status: TypeError: Cannot read properties of undefined (reading 'unscoped')
邮递员回复:
{
"error": "Internal Server Error",
"status": false
}
请帮助如何做到这一点。
leaves
属性 in db
对象可能是 undefined
。
检查 db.leaves
的类型
ex) console.log(typeof db.leaves)
我想通过使用 sequelize
从叶子 table 获取叶子数,但它给我错误未定义(读取 'unscoped')。我正在使用 postgresql 和 NodeJS。
这是我的查询
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 in status: TypeError: Cannot read properties of undefined (reading 'unscoped')
邮递员回复:
{
"error": "Internal Server Error",
"status": false
}
请帮助如何做到这一点。
leaves
属性 in db
对象可能是 undefined
。
检查 db.leaves
ex) console.log(typeof db.leaves)