Display query result to convert as csv

Tourist
6 1 1

Hi guys,

 

I have done a query that fetch the customers of my shop. 

const GET_CLIENTS = gql`
    query{
        customers(first:10){
        edges{
            node{
            email
            acceptsMarketing
            firstName
            state
            updatedAt
            verifiedEmail
            }
        }
        }
    }
`;



class ResourceListWithClients extends React.Component {
  render() {
    return (
      <Page>
        <Query query={GET_CLIENTS}>
          {({ data, loading, error }) => {
            if (loading) return <div>Loading…</div>;
            if (error) return <div>{error.message}C'è stato un errore</div>;
            console.log(data);
            var str = JSON.stringify(data, null, 2);
            var result = [];

            for (var i in data)
              result.push([i, data[i]]);

            console.log(result)[0];
            
            return (
              <Page>
                <Card>Ciao, ho fetchato correttamente i dati</Card>
              </Page>
            );
          }}
        </Query>
      </Page>
    );
  }
}

export default ResourceListWithClients;

Now I have an oriole json file. I want to have just an array with the customers fields (email, accepts_marketing, etc).

 

When I have that array I can convert and download it as a csv file with this code.

// Building the CSV from the Data two-dimensional array
        // Each column is separated by ";" and new line "\n" for next row
        var csvContent = '';
        var dataString;
        data.forEach(function (infoArray, index) {
            dataString = infoArray.join(';');
            csvContent += index < data.length ? dataString + '\n' : dataString;
        });


// The download function takes a CSV string, the filename and mimeType as parameters
                                    // Scroll/look down at the bottom of this snippet to see how download is called
                                    var download = function (content, fileName, mimeType) {
                                        var a = document.createElement('a');
                                        mimeType = mimeType || 'application/octet-stream';

                                        if (navigator.msSaveBlob) { // IE10
                                            navigator.msSaveBlob(new Blob([content], {
                                                type: mimeType
                                            }), fileName);
                                        } else if (URL && 'download' in a) { //html5 A[download]
                                            a.href = URL.createObjectURL(new Blob([content], {
                                                type: mimeType
                                            }));
                                            a.setAttribute('download', fileName);
                                            document.body.appendChild(a);
                                            a.click();
                                            document.body.removeChild(a);
                                        } else {
                                            location.href = 'data&colon;application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported
                                        }
                                    }

                                    download(csvContent, 'dowload.csv', 'text/csv;encoding:utf-8');

 To work, this convertor need the array in this form

var data = [
    ['email1', 'accept_marketing1', 'first_name1'],
     ['email2', 'accept_marketing2', 'first_name2'],
];

 Can someone help me to have the result of the query in an array like this?

0 Likes