首页 > 解决方案 > 当我使用 findById(req.params.id) 时,我的 Sequelize Query 总是做出错误的查询

问题描述

所以我正在尝试进行更新查询,但每次我运行查询时,它总是得到错误的查找 ID。这是我第一次遇到这样的错误。在我的另一个项目中,这永远不会发生

QuestionController.updateQuestionAnswer = async (req, res, next) => {
    try {
        const questionAnswer = await QuestionAnswer.findById(req.params.id)
        console.log(questionAnswer)
        res.status(200).send({
            status_code: 200,
            questionAnswer,
            message: 'Updated'
        })
    } catch(error) {
        console.log(error)
        return res.status(500).send({ status_code: 500, message: error })
    }
}

按 ID 查找错误 在哪里 answer_id

标签: javascriptnode.jspostgresqlsequelize.js

解决方案


您应该将其更改为 WHERE 子句,因为FindById只能在您有一个键(例如 normal Id)时使用,而不是在有多个键(Answer_Id 和 Question_id)或没有键时使用。

const questionAnswer = await QuestionAnswer.where(x => x.question_id == req.params.id)

推荐阅读