javascript - 当我使用 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 })
}
}
解决方案
您应该将其更改为 WHERE 子句,因为FindById
只能在您有一个键(例如 normal Id
)时使用,而不是在有多个键(Answer_Id 和 Question_id)或没有键时使用。
const questionAnswer = await QuestionAnswer.where(x => x.question_id == req.params.id)
推荐阅读
- reactjs - 如何表达 React 组件实例的静态(不可变)状态?
- python - 在python中解析具有大量十六进制字符的json文件
- c++ - 在模板函数中使用
- r - 我无法调试在 git bash 命令行上链接目录路径的问题
- c++ - 该程序没有产生输出,而是卡在一个循环中,几秒钟后消失并打印被杀死?
- java - 如何直接在 localhost:8080 而不是 tomcat 主页中打开我的 Web 应用程序
- javascript - 如何检查用户刚刚输入的内容(区分不同符号的输入)?
- python - 通过 .exe 启动时,Python OpenCV 不读取视频输入
- javascript - node JS - 将函数本身发送到客户端,该函数在客户端(不在服务器中)运行
- c# - 将 ASP .Net WPF 应用程序转换为在没有窗口(窗体)的情况下运行