javascript - 为什么我的 sequelize 查询将十进制列作为字符串返回
问题描述
我正在对十进制列求和,它作为字符串而不是整数返回?
exports.sumMoney = function (id) {
return myModel.findAll({
raw: true,
attributes: [[sequelize.fn('SUM', sequelize.col('moneycol')), 'moneylabel']],
where: { id: id }
}).then(r => r);
}
解决方案
为了将结果转换为数字,您需要使用 sequelize.cast 函数执行显式转换:
exports.sumMoney = function (id) {
return myModel.findAll({
raw: true,
attributes: [[sequelize.cast(sequelize.fn('SUM', sequelize.col('moneycol')), 'int'), 'moneylabel']],
where: { id: id }
}).then(r => r);
}
我不知道为什么在 sequelize v4 文档中没有说明,但 v3 声明:
postgres 需要显式转换。
http://sequelize.readthedocs.io/en/v3/api/sequelize/#fnfn-args-sequelizefn
推荐阅读
- python - 为什么 scipy 贝塞尔根发现不返回零根?
- php - Go & PHP json-rpc 通信
- asp.net-core - 如何:使用默认容器在 ASP.NET Core 中注册 ServiceStack 的 Redis 客户端管理器单例
- python - 用于从多个服务器收集事件日志并生成单个文件并搜索错误的 Python/shell 脚本(首选 Python)
- php - php mysqli 事务不存储第二个查询
- javascript - 在 Jetty Server 上重新加载 JS 和 CSS 文件
- javascript - 如何编码和解码字符串和表情符号
- python-2.7 - PANDAS:写入 Excel 时更改为 1904 日期系统
- python - 使用python转换excle文件中大量时间戳的时区时发现时区转换错误
- javascript - 以编程方式在 dijit 布局 TitlePane 中插入 dijit 内容窗格