gpt4 book ai didi

javascript - 为什么我在 Sequelize 中得到 "sequelize.literal is not a function"

转载 作者:行者123 更新时间:2023-12-02 20:50:58 24 4
gpt4 key购买 nike

我尝试将我的项目克隆到新电脑,然后收到错误调用“sequelize.literal 不是函数”。

这是我的产品 Controller 。我尝试使用文字函数来获取带有 avg_ rating 的产品。

const Product = require("../models").product;

module.exports = {
getProduct: async (req, res) => {
try {
let query = {}
query.where = {
isPublished: 1
};
query.attributes = {
include: [
sequelize.literal(
`(SELECT CAST(AVG(rating) AS DECIMAL(10,1)) FROM product_reviews WHERE productId = \`Product\`.\`id\`)`
),
"avg_rating",
]
}

const product = await Product.findAll(query)

res.send({
status: 200,
data: product,
});

} catch (error) {
res.status(500).send({
error: error.message,
});
}
},
}

查看结果

Status 500 & error: "sequelize.literal is not a function"

这里是我的 package.json。

{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"dev": "nodemon app.js"
},
"author": "NuChaiyakorn",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cookie-session": "^1.4.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-fileupload": "^1.1.7-alpha.3",
"express-promise-router": "^3.0.3",
"faker": "^4.1.0",
"ffmpeg": "0.0.4",
"joi": "^14.3.1",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.10.0",
"mysql2": "^2.1.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"ramda": "^0.27.0",
"randomstring": "^1.1.5",
"sequelize": "^5.21.6",
"slugify": "^1.4.0"
}
}

最佳答案

最后我通过将这一行添加到 Controller 的顶部解决了问题。

const { sequelize } = require('../models/index')

关于javascript - 为什么我在 Sequelize 中得到 "sequelize.literal is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61604873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com