Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing multiple params issue in VUE.JS

I am trying to get data by passing multiple ids as param.But the params i am passing is going as array which i don’t want.

Example:

Current data: The api is returning param as details?ids[]=123&ids[]=245

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Expected data: I would like to pass the param as details?ids=123&ids=245

  const postRes = await axios.post('employee/departments', this.details);
  const getRes = await axios.get('employee/departments',
     { params: { ids: postRes.map(i=>i.id)},
  });
  this.$emit('fileDetails', getRes.data);

>Solution :

You could serialize the parameters yourself:

  1. Map each object into a string, containing 'ids=' prefixed to the object’s id.
  2. Join the resulting array with a & delimiter.
const params = postRes.map(i => 'ids=' + i.id) 1️⃣
                      .join('&') 2️⃣
const getRes = await axios.get('employee/departments?' + params)

Run this snippet for example output:

const postRes = [{ id: 11 }, { id: 22 }, { id: 33 }]
const url = 'employee/departments?' + postRes.map(i => 'ids=' + i.id).join('&')
console.log(url)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading