Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass in an optional parameter in request body conditionally?

I have an optional parameter called activationCodes which is defined as:

(property) PS.Components.Schemas.IC.activationCodes?: string[] | null | undefined

In my request body in my function, I only want to send in activationCodes as a field if another variable string is defined:

I tried it as:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  const reqBody: PS.Components.Schemas.IC = {
    description: input.description,
    outcome: input.outcome,
    activationCodes: aCode ? [aCode] : undefined,
  };

but this results in activationCodes: undefined in the reqBody if aCode is undefined. Is it possible to completely ignore the activationCodes field if aCode is undefined?

>Solution :

Add it after:

const reqBody: PS.Components.Schemas.IC = {
  description: input.description,
  outcome: input.outcome,
};

if (aCode) reqBody.activationCodes = [aCode];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading