Simon Breton Simon Breton - 2 months ago 48
React JSX Question

Passing client-side parameters to the server-side with React/redux and Express (EMPTY_RESPONSE error)

I would like to pass a variable from my client-side to my server-side with react/redux and express. In the example below I'm trying to pass the

VIEW_ID
value to make a google analytics API call. I've been trying to follow some examples given here. When launching my app and after a couple of minute waiting I've got the following error :

GET http://localhost:3000/gadata net::ERR_EMPTY_RESPONSE
dispatchXhrRequest @ bundle.js:34151xhrAdapter @ bundle.js:34003
bundle.js:31131 action @ 15:29:48.843 undefined


I guess I'm missing something on the google analytics API callback.

Here is my action where I'm passing the
VIEW_ID
as a second parameter in my
axios.get
method :

export const fetchgadata = () => (dispatch) => {
dispatch({
type: 'FETCH_DATA_REQUEST',
isFetching:true,
error:null
});

var VIEW_ID = "ga:802840947";

return axios.get("http://localhost:3000/gadata", VIEW_ID)
.then(response => {
dispatch({
type: 'FETCH_DATA_SUCCESS',
isFetching: false,
data: response.data.rows.map( ([x, y]) => ({ x, y }) )
});
})
.catch(err => {
dispatch({
ype: 'FETCH_DATA_FAILURE',
isFetching:false,
error:err
});
console.error("Failure: ", err);
});
};


and here the API call from my server file :

app.use('/gadata', function(req, res, next) {

var VIEW_ID = req.query.VIEW_ID;

var key = require('./client_id.json');

var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/analytics.readonly'], null);

var authorize = function( cb ) {
jwtClient.authorize(function(err) {
if (err) {
console.log(err);
return;
} else {
if ( typeof cb === "function") {
cb();
}
}
});
}
var queryData = function(req, res) {
authorize(function() {
analytics.data.ga.get({
'auth': jwtClient,
'ids': VIEW_ID,
'metrics': 'ga:uniquePageviews',
'dimensions': 'ga:pagePath',
'start-date': '30daysAgo',
'end-date': 'yesterday',
'sort': '-ga:uniquePageviews',
'max-results': 10,
}, function (err, response) {
if (err) {
console.log(err);
return;
}
res.send(response);
});
});
}
});

Answer

CMIIW, i don't think that's how you pass querystring through axios. Try something like this (You can see the axios documentation here, https://github.com/mzabriskie/axios)

axios.get(url, {params: {VIEW_ID: VIEW_ID}})
//or this
axios.get(url + '?VIEW_ID=' + VIEW_ID)
Comments