Skip to main content
added 184 characters in body
Source Link
Roxy'Pro
  • 159
  • 1
  • 5
let formData = new FormData();
  formData.append('File', document.file);
  formData.append('DocumentType', document.documentType);

  product = {
    ...values,
    productType: productT,
  };

  for (var key in product) {
    if (key != 'createdDate' && key != 'expirationDate') {
      formData.append(key, product[key]);
    } else {
      formData.append(key, new Date(product[key]).toUTCString());
    }
  }

  await createproduct(formData);

I need to send file and other data about product in one request so I added all the stuffs in FormData, and I have issues only with Dates, I must convert it to date because somehow it get confused if I don't convert it.

Can I somehow refactor this code to do this part about dates more elegant, if condition in for loop somehow smells bad..

P.S EDIT:

for (var key in product) {
   const transform = DateTransformer[key];
   formData.append(key, transform ? transform[product[key]] : product[key]);
}
let formData = new FormData();
  formData.append('File', document.file);
  formData.append('DocumentType', document.documentType);

  product = {
    ...values,
    productType: productT,
  };

  for (var key in product) {
    if (key != 'createdDate' && key != 'expirationDate') {
      formData.append(key, product[key]);
    } else {
      formData.append(key, new Date(product[key]).toUTCString());
    }
  }

  await createproduct(formData);

I need to send file and other data about product in one request so I added all the stuffs in FormData, and I have issues only with Dates, I must convert it to date because somehow it get confused if I don't convert it.

Can I somehow refactor this code to do this part about dates more elegant, if condition in for loop somehow smells bad..

let formData = new FormData();
  formData.append('File', document.file);
  formData.append('DocumentType', document.documentType);

  product = {
    ...values,
    productType: productT,
  };

  for (var key in product) {
    if (key != 'createdDate' && key != 'expirationDate') {
      formData.append(key, product[key]);
    } else {
      formData.append(key, new Date(product[key]).toUTCString());
    }
  }

  await createproduct(formData);

I need to send file and other data about product in one request so I added all the stuffs in FormData, and I have issues only with Dates, I must convert it to date because somehow it get confused if I don't convert it.

Can I somehow refactor this code to do this part about dates more elegant, if condition in for loop somehow smells bad..

P.S EDIT:

for (var key in product) {
   const transform = DateTransformer[key];
   formData.append(key, transform ? transform[product[key]] : product[key]);
}
Source Link
Roxy'Pro
  • 159
  • 1
  • 5

Adding values to FormData - javascript

let formData = new FormData();
  formData.append('File', document.file);
  formData.append('DocumentType', document.documentType);

  product = {
    ...values,
    productType: productT,
  };

  for (var key in product) {
    if (key != 'createdDate' && key != 'expirationDate') {
      formData.append(key, product[key]);
    } else {
      formData.append(key, new Date(product[key]).toUTCString());
    }
  }

  await createproduct(formData);

I need to send file and other data about product in one request so I added all the stuffs in FormData, and I have issues only with Dates, I must convert it to date because somehow it get confused if I don't convert it.

Can I somehow refactor this code to do this part about dates more elegant, if condition in for loop somehow smells bad..