1

i'm trying to send list of data using form_data in react-native , but it is posting first data only , how to achieve that in react native ,

My Data ,

var data = ["person_1","person_2","person_3"];

My Code ,

export function updateUsers(data,token) {
  const fd = new FormData();
  fd.append('first_name', data); // here i need to update all the data
  
  return dispatch => {
                 axios({
                   method : 'patch',
                   url : SERVICE.url+"user/"+un+"/",
                   headers: {
                     'Authorization' : "Token "+token,
                  },
                   data : fd
                })
                .then(response => {
                     console.log("updateUser done  . . .");
                     var data = response.data;
                     dispatch({
                       type: "UPDATE_USER",
                       data,
                     });
                   }).catch(error => {
                     console.log("got error in UPDATE_USER ", error);
                   });
          }
}

but for now , only one data is updated at a time , so how can i achieve that .help me from this .

8
  • You have data as an array, so do you want for each item in array send a request? Commented Oct 16, 2017 at 20:24
  • Yah , absolutely bro !! Commented Oct 16, 2017 at 20:40
  • i mean , single post request with array of data . Commented Oct 16, 2017 at 20:40
  • Did you try convert object to string? Connection between backend and frontend should be on string. Try data: JSON.stringify(fd) Commented Oct 16, 2017 at 20:46
  • Tried bro , But no luck . Commented Oct 16, 2017 at 21:58

1 Answer 1

4

You should loop through each of your data and append it into your formdata's first_name field.

data.forEach((item) => {
   fd.append('first_name', item)
})

The remaining code will be same.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.