简体   繁体   中英

Node.js rest api with json and bigint properties

I'm quite new to node.js and I'm working in a rest api in which I'm having an annoying problem that I just can't wrap my head around.

This is the problem: the api recieves, via POST, a json object. Within this json object there are some properties that are bigint. Let me give you an example of these objects:

{
  "items": [
    {
      "appid": 730,
      "classid": "2222",
      "id": 99,
      "instanceid": "instanceID",
      "market_name": "Market name"
    },
    {
      "appid": 730,
      "classid": "2222",
      "id": 99,
      "instanceid": "instanceID",
      "market_name": "Market name"
    }
  ],
  "message": "Mensagem de teste",
  "user": 76561197960275584
}

In the server side the user property returns 76561197960275580 . It is a JS rounding problem and I have tried to solve it using many packages (bignumber.js, json-bigint, etc.. ) in total some 5 solutions and none have worked for me. I just can't use them because when the json object hits the server it is already with its int properties rounded. Let me put a piece of my node code here.

var express = require('express');
var bodyParser = require('body-parser');
var api = express();

api.use(bodyParser.urlencoded({ extended: false }));
api.use(bodyParser.json());

api.post('/import', function importEndPoint(req, res) {

    console.log('req.body.user=' + req.body.user);
});

When I console.log(req.body) the values are already rounded.

As I told, I've tried many libs to solve. I've read a lot of answers to problems like mine and still it doesn't work. I'm sure it is a newbie thing I'm missing but I just can't realize what it is.

Can you help me, please? The api uses express 4 and I can't change the json object to send these values with string.

Thanks in advance for any help.

使该字段字符串成为您自己的名字的最简单的解决方案,否则您必须修改正文解析器,并更改转换此字段的方式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM