![](/img/trans.png)
[英]Sequelize - How can I return JSON objects of the database results only?
[英]How to return sequelize sum as json?
如何在json中返回值?
exports.get = id => Model.sum('price',
{
where: {
id,
}
}
);
结果
23
期望
{ sum: 23 }
您可以通过2种方式做到这一点:
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 }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.