【问题标题】:NodsJs: TypeError: Cannot read properties of undefined (reading 'unscoped')NodsJs:TypeError:无法读取未定义的属性(读取“无范围”)
【发布时间】:2022-01-13 14:20:07
【问题描述】:

我想通过使用sequelize 从叶子表中获取叶子数,但它给了我未定义的错误(读取'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
}

请帮助如何做到这一点。

【问题讨论】:

    标签: node.js postgresql sequelize.js


    【解决方案1】:

    db 对象中的leaves 属性可能是undefined。 检查db.leaves的类型

    例如)console.log(typeof db.leaves)

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 1970-01-01
      • 2023-03-11
      • 2022-01-15
      • 2021-12-31
      • 2021-12-13
      • 2021-11-28
      • 2021-11-15
      • 2021-11-06
      相关资源
      最近更新 更多