Skip to content

"Duplicate key" error when parsing array-like field with non-numeric key #2

@michaelrog

Description

@michaelrog

Thanks so much for this package!

We're trying to parse FormData from fields that contain associative-array content:

<input type="hidden" name="myField[foo]" value="1">
<input type="hidden" name="myField[bar]" value="2>

Minimal reproduction:

import {parseFormData} from 'parse-nested-form-data'

const formData = new FormData()
formData.append('myField[foo]', '1')
formData.append('myField[bar]', '2')
parseFormData(formData)

Using string keys with array fields like this produces a "Duplicate key" error:

Uncaught Error: Duplicate key at path part array[foo]
    DuplicateKeyError parse-nested-form-data.esm.js:42
    handlePathPart parse-nested-form-data.esm.js:237
    parseFormData parse-nested-form-data.esm.js:363
    parseFormData parse-nested-form-data.esm.js:362

I'm not surprised to see an error here, since JS [] arrays are integer-indexed, but... Given that string-keyed "array" fields are pretty common in forms/apps these days, could parseFormData() be somehow clever enough to parse these entries into a map object instead of a numeric-keyed array?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions