1

I'm trying to implement a delete function where the user can select multiple files, then I will retrieve both the fileID and the deletedBy field. After mapping both fileID and deletedBy together, I will send them to this deleteFile function where in this case I need to send 3 axios get requests accordingly and individually to delete the function, but I just can't make the loop work.

   const deleteFile = async (file) => {
        const responses = [];
        const url = 'https://api.com/delete_file_web';

        for (let i = 0; i < file.length; i++) {
            responses.push(await axios.get(url, { params: {
                fileID: file.fileID,
                deletedBy: file.deletedBy
            }})
            )
        }

        Promise.all(responses)
        .then(response =>
            alert('File has been successfully deleted!')
        )
        .catch(err => console.error(err))
    };

enter image description here

2
  • 1
    Fix these: fileID: file[i].fileID and deletedBy: file[i].deletedBy Commented Aug 19, 2021 at 9:08
  • 1
    Unfortunately it still doesn't work, is there something wrong with my axios request instead? Commented Aug 19, 2021 at 9:13

1 Answer 1

1

Just use i in loop.

const deleteFile = (file) => {
    const requests = [];
    const url = 'https://api.com/delete_file_web';

    for (let i = 0; i < file.length; i++) {
        requests.push(axios.get(url, { params: {
            fileID: file[i].fileID,
            deletedBy: file[i].deletedBy
        }})
        )
    }

    axios.all(requests)
        .then((res) => {
            console.log(res);
        });
};
Sign up to request clarification or add additional context in comments.

9 Comments

Unfortunately it still doesn't work, is there something wrong with my axios request instead?
@Lance Make sure you are not using await keyword in front of axios.get.
@Danial Sorry, I've tried your method but the console returns undefined and I still can't get it to work...
I test the code with a public API and did work. Are you sure about your api?
|

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.