astrakid932
astrakid932

Reputation: 3415

Error with FormData.entries() in React Native

I am trying to convert FormData to simple JSON format such as {“val1” :1 , “val2” : 3} to send to API via fetch(). But, currently FormData is a multi array

I get error entries() is not a function error

    const {email, password, fname} = this.state;
    var formData = new FormData();
    formData.append('email', email);
    formData.append('password', password);
    formData.append('fname', fname);

    this.setState({spinner: true});

    let jsonObject = {};

    for (const [key, value]  of formData.entries()) {
        jsonObject[key] = value;
    }

Upvotes: 2

Views: 1029

Answers (1)

bug
bug

Reputation: 4150

Use formData.getParts(), eg:

let jsonObject = {};

for (const part of formData.getParts()) {
    jsonObject[part.fieldName] = part.string;
}

Upvotes: 3

Related Questions