node.jsexpresspostmanmultipartform-datamulter

Express: Parsing array of strings in multipart/form data


I am trying to send an array of type String and a png file using multipart/form data. I receive my image file and I receive my data via req.body. However the array is of type String, as expected. How do I parse this into an array?

Here's my postman request:

enter image description here

Here's my console log:

enter image description here


Solution

  • You could use JSON.parse to convert string to array.

    let tattooGroups= `["Women", "Unisex"]`;
    console.log("Without JSON.parse ->  ", typeof tattooGroups);
    console.log("With    JSON.parse -> ", typeof JSON.parse(tattooGroups));
    console.log("With    JSON.parse, Is Array -> ", Array.isArray(JSON.parse(tattooGroups)));
    
    console.log(JSON.parse(tattooGroups));