我想通过react / redux和express将变量从客户端传递到服务器端。在下面的示例中,我尝试传递该VIEW_ID
值以进行Google Analytics(分析)API调用。我一直在尝试遵循此处给出的一些示例。当启动我的应用程序并等待几分钟后,出现以下错误:
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
我想我在Google Analytics(分析)API回调中遗漏了一些东西。
这是我VIEW_ID
在axios.get
方法中将第二个参数传递给我的动作:
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);
});
};
这是我服务器文件中的API调用:
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);
});
});
}
});
CMIIW,我不认为这是您通过axios传递查询字符串的方式。尝试这样的事情(您可以在此处查看axios文档,https://github.com/mzabriskie/axios)
axios.get(url, {params: {VIEW_ID: VIEW_ID}})
//or this
axios.get(url + '?VIEW_ID=' + VIEW_ID)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句