2016-09-07 4 views
0

クライアント側からサーバー側にreact/reduxと変数を渡して表現したいと思います。下の例では、GoogleアナリティクスAPI呼び出しを行うためにVIEW_ID値を渡そうとしています。私はhereを与えられたいくつかの例に従おうとしています。私のアプリを起動し、分待ちのカップルの後、私は次のエラー持っているとき:クライアント側のパラメータをReact/reduxとExpress(EMPTY_RESPONSEエラー)を使用してサーバー側に渡す

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コールバックで何かが欠けてると思いますが。

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の呼び出し::ここで

は私axios.get方法で2番目のパラメータとしてVIEW_IDを渡している私の行動である

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

答えて

2

CMIIW、私はそれがあなたが斧を渡す方法だとは思わない。このような何かを試してみてください

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

はいおかげで(ここにaxiosドキュメント、https://github.com/mzabriskie/axiosを見ることができます)。あなたはまったく正しい。私はreqとparamsがどのようにリンクしていたのか理解できませんでしたが、今はもっと明確です! –

関連する問題