Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在POST请求期间正在修改对象的结构_Node.js_Express_Needle.js - Fatal编程技术网

Node.js 在POST请求期间正在修改对象的结构

Node.js 在POST请求期间正在修改对象的结构,node.js,express,needle.js,Node.js,Express,Needle.js,因此,我使用针发送post请求,并将以下对象作为主体: { time: 1533725993910, rconPort: 52940, rconPassword: 'muqrxllv', serverPort: 38950, unique: 795571399, publicIP: 'localhost', mods: [ { modName: 'clusterio_1.0.0.zip', hash: '341feb6c60918c83f7f3a6a1

因此,我使用针发送post请求,并将以下对象作为主体:

{ time: 1533725993910,
  rconPort: 52940,
  rconPassword: 'muqrxllv',
  serverPort: 38950,
  unique: 795571399,
  publicIP: 'localhost',
  mods:
   [ { modName: 'clusterio_1.0.0.zip',
       hash: '341feb6c60918c83f7f3a6a14a4a308aef18dda6' },
     { modName: 'clusterio_1.13.1.zip',
       hash: '6124bb9d3896b030fafbef07c971314f4640759f' },
     { modName: 'clusterio_1.13.0.zip',
       hash: '3dde5199f63d87e3e6ea6ef7f522e0df5842dee4' },
     { modName: 'hotpatch-multimod_1.1.4.zip',
       hash: '0a780e23c42452bd51ecc5ce808b65842e77dc85' },
     { modName: 'creative-mode_0.0.1.zip',
       hash: '9af437c10d3f39f1f3ffb57c942c3a51d3786f0f' },
     { modName: 'clusterio_1.12.0.zip',
       hash: '959c82380cad1a6f039de401a98dd70e13ca2224' } ],
  instanceName: 'test',
  playerCount: '0' }
当我在Express处理程序中输入console.log(req.body)时,我得到

{ time: '1533722833600',
  rconPort: '52940',
  rconPassword: 'muqrxllv',
  serverPort: '38950',
  unique: '795571399',
  publicIP: 'localhost',
  mods: [ { modName: [Array], hash: [Array] } ],
  instanceName: 'test',
  playerCount: '0',
  mac: '00-FF-8C-0F-21-F0' }
经过仔细检查,“mods”字段现在看起来如下所示:

"mods":[{
  "modName":[
    "clusterio_1.0.0.zip",
    "clusterio_1.13.0.zip",
    "clusterio_1.13.1.zip",
    "hotpatch-multimod_1.1.4.zip",
    "creative-mode_0.0.1.zip",
    "clusterio_1.12.0.zip"
  ],"hash":[
    "341feb6c60918c83f7f3a6a14a4a308aef18dda6",
    "3dde5199f63d87e3e6ea6ef7f522e0df5842dee4",
    "6124bb9d3896b030fafbef07c971314f4640759f",
    "0a780e23c42452bd51ecc5ce808b65842e77dc85",
    "9af437c10d3f39f1f3ffb57c942c3a51d3786f0f",
    "959c82380cad1a6f039de401a98dd70e13ca2224"
]}]
a=1
b[c]=2
b[d][0]=3
b[d][1]=4
e[0]=5
e[1]=6
用于在服务器端记录数据,我使用

var app = express();
app.use(bodyParser.json());
app.use((req,res,next) => {
    if(req.originalUrl == "/api/getID"){
        console.log(req.body)
    }
    next()
});
在客户端,我是这样做的

console.log(payload);
needle.post('localhost:8080/api/getID', payload, needleOptionsWithTokenAuthHeader, function (err, response, body) {
起初我怀疑这是中间件的一个问题,但由于我在所有中间件加载之前就记录了日志,我对此不再太确定


我从来没有经历过这样的事情,如果有任何帮助,我将不胜感激。

您是否尝试过使用函数
JSON.stringify
()? 用表格式化POST请求可能很棘手

要发送像
{a:1,b:{c:2,d:[3,4]},e:[5,6]}
这样的对象,必须格式化参数以适合关联数组,因此参数如下所示:

"mods":[{
  "modName":[
    "clusterio_1.0.0.zip",
    "clusterio_1.13.0.zip",
    "clusterio_1.13.1.zip",
    "hotpatch-multimod_1.1.4.zip",
    "creative-mode_0.0.1.zip",
    "clusterio_1.12.0.zip"
  ],"hash":[
    "341feb6c60918c83f7f3a6a14a4a308aef18dda6",
    "3dde5199f63d87e3e6ea6ef7f522e0df5842dee4",
    "6124bb9d3896b030fafbef07c971314f4640759f",
    "0a780e23c42452bd51ecc5ce808b65842e77dc85",
    "9af437c10d3f39f1f3ffb57c942c3a51d3786f0f",
    "959c82380cad1a6f039de401a98dd70e13ca2224"
]}]
a=1
b[c]=2
b[d][0]=3
b[d][1]=4
e[0]=5
e[1]=6
要填充非关联数组,可以尝试
e[]=7
表示法