1

Hi I have stored an Array of strings into a state object, when I am trying to retrieve it as appended string, it is giving me appended objects as below, my code is as below and I am getting the result as below:

    handleDownload = (e) => {
        e.preventDefault();

        var formData = new FormData();
        formData.append('communityname', this.state.selectedCommunity);
        formData.append('files', this.state['files']);

        alert(this.state['files'].join(','));
        let url = clientConfiguration['filesApi.local'];

        axios({
            method: 'post',
            url: url,
            data:  formData
        });
    }
My Results are coming as below:

[object Object],[object Object],[object Object],[object Object]

Any help would be very much appreciated - thanks in advance

1
  • This has nothing to do with react Commented Oct 14, 2019 at 16:35

1 Answer 1

1

You can stringify it so that you can properly read the values. Try this:

alert(JSON.stringify(this.state['files']));

The JSON.stringify() method converts a JavaScript object or value to a JSON string. In your case, it will convert the array of objects to JSON.

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

4 Comments

Thank you it did work, but just asking, is there anyway I can just post Array as it is to the Api, instead of converting them into json.stringify, I would like that, if there is any. Can you please suggest me something buddy. And thanks a lot and much appreciated for the earlier help - thank you from the heart.
Since you are posting an array of objects, you really want to be sending JSON instead of the actual array. You will just need to make sure your server knows how to handle it. A lot of backend languages and frameworks will handle it for you automatically, and will know exactly what to do with the JSON data that you sent.
I'm glad my original answer on JSON.stringify() helped you! If you don't mind, could you vote it up and mark it as the correct answer? That would be awesome. Hope you have a great day!
Yes I did, you too my friend

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.