首页 > 解决方案 > req.body 无法解析来自名称属性数组的输入

问题描述

我有一个带有一些名称属性数组的表单。目前,我有两个问题。

(1) 提交表单后,我无法解析 itemName[]、itemValue[]、itemQuantity[] 中的数组值。

(2) 我不明白为什么 itemName[], itemValue[], itemQuantity[] 是对象中的字符串。

谁能告诉我我做错了什么?

{ sessionUser: '5ae767331e2086537a6026be',
  senderName: 'Mr. Camden Denesik',
  senderPhone: '411-431-6729 x28435',
  senderEmail: 'Pink.Boyer5@yahoo.com',
  senderAddress: '3324 Marvin Hollow',
  senderCity: 'South Ariannaland',
  senderProvince: 'Iowa',
  senderPostalCode: '61394-2622',
  senderCountry: 'Libyan Arab Jamahiriya',
  recipientName: 'Mac Keeling',
  recipientIdNumber: '17462',
  recipientPhone: '009-180-3472',
  recipientEmail: 'Lucas_Schultz67@gmail.com',
  recipientAddress: '91928 Bobbie Port',
  recipientCity: 'Catharineside',
  recipientProvince: 'Iowa',
  recipientPostalCode: '80415',
  recipientCountry: 'Montserrat',
  'itemName[]': [ 'microchip', 'Kaka' ],
  'itemValue[]': [ '135.00', '100' ],
  'itemQuantity[]': [ '95065', '20' ],
  itemTotalValue: '4.00',
  insurance: 'on',
  cargoInsurance: '893.00' }

标签: javascripthtmlnode.jsexpresshandlebars.js

解决方案


(2) This is how javascript handle attributes with special characters, try accessing the variables like a dictionary array obj['itemName[]']. The best way to avoid this is to remove the [] from the attribute names.

As for (1) I don't fully understand your problem but I guess it has something to do with the inability to access the variables.


推荐阅读