node.js - 将 Sequelize 中的字段相乘选择

标签 node.js sequelize.js

我有以下代码片段:

    const buys = await Buy.findAll({
      where: {
        account_id: req.params.id,
      },
      attributes: ['id', 'account_id', 'buy_value', 'quantity'],
    });

    return res.json(buys);
它返回以下 JSON:
{
    "id": 9,
    "account_id": 3,
    "buy_value": "49521.00",
    "quantity": 2
}
我想在同一个 JSON 中返回一个 test_field 乘以 buy_valuequantity 字段。我该怎么做 ?

最佳答案

只需像这样使用 Sequelize.literal :

const buys = await Buy.findAll({
      where: {
        account_id: req.params.id,
      },
      attributes: ['id', 'account_id', 'buy_value', 'quantity', [Sequelize.literal('buy_value*quantity'), 'test_field']],
    });

关于node.js - 将 Sequelize 中的字段相乘选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63022690/

相关文章:

node.js - 使用 Axios 在 Sequelize 上更新请求 (PUT) 超时

javascript - #Sequelize 同时添加检索所有属性

node.js - Chai http - 双重回调问题

javascript - 如何在输入框下方添加文字?

mysql - Sequelize 获取 MySql 存储过程数据作为数组?

model - 在 Sequelize 中使用模型和迁移

node.js - heroku 上缺少 Access-Control-Allow-Origin header

node.js - PEM 证书给出 "wrong tag"或 "header too long"错误

javascript - Mongodb异步获取结果

node.js - 更改模型后如何创建 Sequelize cli db 迁移