javascript - How to post Array of data for FormData using react-native - Stack Overflow

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

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);
                   });
          }
}

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 .

Share Improve this question edited Oct 23, 2017 at 14:27 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Oct 16, 2017 at 20:01 Beckham_VinothBeckham_Vinoth 7213 gold badges14 silver badges40 bronze badges 8
  • You have data as an array, so do you want for each item in array send a request? – Nurlan Mirzayev Commented Oct 16, 2017 at 20:24
  • Yah , absolutely bro !! – Beckham_Vinoth Commented Oct 16, 2017 at 20:40
  • i mean , single post request with array of data . – Beckham_Vinoth 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) – Nurlan Mirzayev Commented Oct 16, 2017 at 20:46
  • Tried bro , But no luck . – Beckham_Vinoth Commented Oct 16, 2017 at 21:58
 |  Show 3 more ments

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745634492a4637307.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信