我如何在json中返回值?
exports.get = id => Model.sum('price',
{
where: {
id,
}
}
);
结果
23
所需的
{ sum: 23 }
最佳答案
您可以通过两种方式做到这一点:
1. 简单更改响应:
exports.get = id => Model.sum('price',
{
where: {
id,
}
}
).then(sum => {sum}); //<----- Little hack
2. 使用 sequelize.fn ,下面会返回 array ,所以你需要返回 array 的第一个元素,以获得预期的结果
exports.get = id => Model.findAll({
attributes: [[sequelize.fn('SUM', sequelize.col('price')), 'sum']] ,
where : { id }
});
关于javascript - 如何将 Sequelize 总和作为 json 返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52023459/