Home > OS >  Express: cannot access an array of numbers even though it is in the request body
Express: cannot access an array of numbers even though it is in the request body

Time:11-04

Express / Typescript:


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!' })

}); 

the console log for req.body is as follows:

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 ]
}

As you can see, there are fields updatedQuota and updatedItems. One is an array of strings and the other is an array of numbers;

However, after the destructoring line const { organizationId, formdata, updatedItems, updateQuota } = req.body; , some of fields are fine, but some fields becomes undefined:

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

if I access the req.body.updateQuota alone by printing it console.log("req.body.updateQuota: ", req.body.updateQuota);, it is also empty:

req.body.updateQuota:  undefined

Why is it that things are clearly contained in req.body, but cannot be accessed or extracted?

CodePudding user response:

formData or formdata ?

updatedQuota or updateQuota ?

CodePudding user response:

req.body has updatedQuota not "updateQuota"

  • Related