1

Getting wrong error message

console.log(error)

devtools network Response

const handleDownloadPicture = async (ids: number) => {
    try {
        const response = await axios
            .post(`/api/folders/${folderDetails.id}/download`, {
                    resources: ids,
                },{responseType:'blob'},
            )
            .then(({ data: blob }) => {
                const link = document.createElement('a');
                const url = URL.createObjectURL(blob);
                link.href = url;
                link.download = 'myfile.zip';
                link.click();
            });
    } catch (error) {
        console.error(error);

    }
};

I'm getting the wrong error message on console.log(error)

have tried multiple solutions of converting blob response to json but no result

Martin54
  • 1,349
  • 2
  • 13
  • 34
razer
  • 108
  • 2
  • 18

2 Answers2

1

axios.post(`/api/folders/${folderDetails.id}/download`, {
    resources: ids,
  },{
    responseType:'blob'
  }).then(({ data: blob }) => {
    const link = document.createElement('a');
    const url = URL.createObjectURL(blob);
    link.href = url;
    link.download = 'myfile.zip';
    link.click();
  }).catch(async (error) => {
    console.log(JSON.parse(await error.response.data.text()));
  });
0

Becasue axios.post() is return Promise, you should use once in two way to get response or catch error: const response = await axios.post() or axios.post().then(response).catch().

You should use only once in two way:

Case 1:

    try {
       const response = await axios.post(`/api/folders/${folderDetails.id}/download`)
       ....
      } catch(error){
      console.log(error) // <=== Error will be catch here!
      }

Case 2:

axios.post(`/api/folders/${folderDetails.id}/download`, {
     resources: ids,
   },{responseType:'blob'},
   ).then(({ data: blob }) => {
       const link = document.createElement('a');
       const url = URL.createObjectURL(blob);
       link.href = url;
       link.download = 'myfile.zip';
       link.click();
     }).catch((error) => console.log(error) //<=== Error will be catch here!
VMT
  • 789
  • 1
  • 3
  • 11
  • thanks but my question is why am i getting different error. how to catch errors shown on image above? – razer May 17 '22 at 05:30