【问题标题】:Express: cannot access an array of numbers even though it is in the request bodyExpress:无法访问数字数组,即使它在请求正文中
【发布时间】:2021-12-18 10:10:04
【问题描述】:

快递/打字稿:


export const updateSettings = catchErrors(async (req, res) => {

  console.log("updateSettings req.body: ", req.body)
  
  const { organizationId, formdata, updatedItems, updateQuota } = req.body; 

  console.log("organizationId: ", organizationId);
  console.log("formdata: ", formdata);
  console.log("updatedItems: ", updatedItems);
  console.log("updateQuota: ", updateQuota);
  console.log("req.body.updateQuota: ", req.body.updateQuota);

  res.respond({'message': 'Settings updated successfully!' })

}); 

req.body 的控制台日志如下:

updateSettings req.body:  {
  username: 'staff_a',
  organizationId: '2',
  formData: '{"offDays":[0,6,1],"officeHourStart":"1899-12-31T09:00:00.000+09:00","officeHourEnd":"1899-12-31T18:00:00.000+09:00","displayLanguage":"cn","timezone":"beijing","doctorQuotas":{"5":5,"6":10}}',
  updatedItems: [ 'displayLanguage', 'timezone', 'billingClosingDay', 'offDays' ],
  updatedQuota: [ 6, 5 ]
}

如您所见,有updatedQuota 和updatedItems 字段。一个是字符串数组,另一个是数字数组;

但是,在析构行const { organizationId, formdata, updatedItems, updateQuota } = req.body; 之后,有些字段没问题,但有些字段变得未定义:

organizationId:  2
formdata:  undefined
updatedItems:  [ 'displayLanguage', 'timezone', 'billingClosingDay', 'offDays' ]
updateQuota:  undefined

如果我通过打印 console.log("req.body.updateQuota: ", req.body.updateQuota);单独访问req.body.updateQuota,它也是空的:

req.body.updateQuota:  undefined

为什么req.body中明明有东西,却无法访问或提取?

【问题讨论】:

  • 错字:updateQuotaupdatedQuota, formDataformdata

标签: javascript node.js typescript express


【解决方案1】:

formData 或 formdata ?

updatedQuota 或 updateQuota ?

【讨论】:

    【解决方案2】:

    req.body 已更新配额而不是“updateQuota”

    【讨论】:

      【解决方案3】:
      {username, organizationId, formData, updatedItems, updateQuota} = req.body
      

      【讨论】:

        猜你喜欢
        • 2021-05-27
        • 1970-01-01
        • 2019-11-14
        • 2019-04-13
        • 2021-07-07
        • 1970-01-01
        • 1970-01-01
        • 2016-08-01
        • 2019-04-07
        相关资源
        最近更新 更多