4

I have an event like below:

    handleDownload(e) {
    e.preventDefault();
    alert('Hi');
    let communityName = this.state['selectedCommunity'];
    let files = this.state[files];

    fetch(clientConfiguration['filesApi.local'], {
        method: 'POST',
        headers: new Headers(),
        body: JSON.stringify({ communityName: communityName, body: files })
    }).then((res) => res.json())
        .then((data) => console.log(data))
        .catch((err) => console.log(err))
};

I have a button as below:

    renderDownloadButton() {
    if (this.state.files && this.state.files.filter(i => i.checked).length) {
        return (
            <button id="download" styles="display: none;" onClick={this.handleDownload} >
                Download
    </button>
        );
    }
};

It fires but it is giving following error, any help please - thank you. At

let communityName = this.state['selectedCommunity'];

its giving me the error;

Can not read property state undefined

Any help please?

3
  • could you show me about your errors? Commented Oct 10, 2019 at 22:04
  • i have edited with the error that I am getting my friend Commented Oct 10, 2019 at 22:07
  • I am not sure about your state of the component. Please share your component code fully. Commented Oct 10, 2019 at 22:14

2 Answers 2

1

My guess is that you need to bind your handler, but it's really hard to tell without whole component code.

handleDownload = (e) => {
    e.preventDefault();
    alert('Hi');
    let communityName = this.state['selectedCommunity'];
    let files = this.state[files];

    fetch(clientConfiguration['filesApi.local'], {
        method: 'POST',
        headers: new Headers(),
        body: JSON.stringify({ communityName: communityName, body: files })
    }).then((res) => res.json())
        .then((data) => console.log(data))
        .catch((err) => console.log(err))
}
Sign up to request clarification or add additional context in comments.

3 Comments

But why is it giving error in reading the state? let communityName = this.state['selectedCommunity']; and how can I pass the file name that files state to the api, any idea buddy?
Because if you have function handleDownload(e) then you switch context, therefore "this" means function context. If you use arrow function then "this" means class / component context.
Thanks a lot it did work for me, but the Api post is not receiving the values community name and files in the back-end
0

For the api post not fetching the error can be one possible case of CORS error that browser do not allow you to access other network(private and secured IP address) so you need to actually allow the proxy setting as I can see your post data don't have any proxy enabled. Here is the code I am attaching

This is the pseudo code please make changes accordingly in your fetch method:

    var targetUrl ='/downloadableReport'
        const res= fetch(targetUrl,{
            method: 'POST',
            headers: {
                        'Content-Type': "application/json; charset=utf-8",
            },
            body: JSON.stringify({
                              
                "requestData":{
                   "userName":"XXX",
                   "password":"XXXX",
                   "clientId":"XXXX",
                   "txnType":"XXXX"
                }
            })
        })
        .then(response => response.json())
        .catch(error =>{
                console.log(error)
            })
    

Also, you need to add setupProxy.js file (mind the name of the file should be this only) and add this code (with preferred changes)

    const proxy = require("http-proxy-middleware");
    module.exports = function(app) {
        app.use(
            proxy("/downloadableReport",{
                target:"http://192.168.1.220:8080/report/abc" ,
                changeOrigin:true
            })
        )
    };

1 Comment

I changed a little bit and it is now posting one parameter that's string, but when 2nd parameter is array of strings, it not posting correctly, it is posting array of string as array of objects, any help please, below is my code for it: handleDownload = (e) => { e.preventDefault(); var formData = new FormData(); formData.append('communityname', this.state.selectedCommunity); formData.append('files', files); let url = clientConfiguration['filesApi.local']; axios({ method: 'post', url: url, data: formData }); } files here is array strings, any help please.

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.