Sequelize 将整数作为字符串返回 [英] Sequelize is returning integer as string

查看:32
本文介绍了Sequelize 将整数作为字符串返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有 sequelize 的 nodejs v4,我有一个这样的模型:

I using nodejs v4 with sequelize, and I have a model like this:

var Device = sequelize.define('Device', {
id: {
  type: DataTypes.BIGINT,
  primaryKey: true,
  autoIncrement: true
},
tenantId: {
  type: DataTypes.BIGINT,
  allowNull: false
},
token: {
  type: DataTypes.STRING,
  allowNull: false
}
}, {
 tableName: 'devices'
});

当我通过 id 选择设备时,id 的类型是一个字符串,例如:

When I select a device by id the type of id is a string, exemple:

Device.findById(9).then( function(result) {
  console.log(result.toJSON().id + 10);
});

输出将是 910,而不是 19,所以我查看 json 并看到了这个:

The output will be 910, rather than 19, so I look at json and a saw this:

{
  id: "9"
  tenantId: "123"
  token: "adsadsdsa"
}

找到的设备中的 id 是一个字符串,但我将其定义为数字...

The id in found device is a string, but I defined it as a number...

不应该是 { "id": 9 } 吗?

Doesn't it should be { "id": 9 } ?

如何选择具有我之前定义的类型的设备?

How can I select a device with the types that I defined previously?

推荐答案

我在 sequelize repo 上找到了解决此问题的方法.

I found a fix to this problem on sequelize repo.

https://github.com/sequelize/sequelize/issues/4523

用于 sequelize 的 pg 模块以字符串形式返回 bigint,因为不能保证 bigint 适合 js 数字.所以我将模型更改为使用整数 (DataTypes.INTEGER)

The pg module used for sequelize returns bigint as string because bigints are not guaranteed to fit into js numbers. So I change my model to use integer (DataTypes.INTEGER)

这篇关于Sequelize 将整数作为字符串返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆