【问题标题】:Passing client-side parameters to the server-side with React/redux and Express (EMPTY_RESPONSE error)使用 React/redux 和 Express 将客户端参数传递到服务器端(EMPTY_RESPONSE 错误)
【发布时间】:2016-09-07 20:44:10
【问题描述】:

我想通过 react/redux 和 express 将一个变量从我的客户端传递到我的服务器端。在下面的示例中,我尝试传递 VIEW_ID 值来进行谷歌分析 API 调用。我一直在尝试遵循给定here 的一些示例。启动我的应用程序并等待几分钟后,出现以下错误:

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 

我想我在谷歌分析 API 回调中遗漏了一些东西。

这是我在 axios.get 方法中将 VIEW_ID 作为第二个参数传递的操作:

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);
    }); 
  });
}
});

【问题讨论】:

    标签: node.js express reactjs get


    【解决方案1】:

    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)
    

    【讨论】:

    • 是的,谢谢。你是完全正确的。我不明白 req 和 params 是如何关联的,但现在更清楚了!
    猜你喜欢
    • 2016-09-18
    • 2016-06-17
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多