[英]Getting 200 status code but response is undefined in react
/*global gapi*/
import React,{Component} from 'react';
import Dashboard from './Dashboard';
import MetaTags from 'react-meta-tags';
import {connect} from 'react-redux';
class AddWebsite extends Component{
constructor(props){
super(props);
}
queryAccounts() {
console.log('inside queryAccounts');
gapi.client.load('analytics', 'v3').then(function() {
gapi.client.analytics.management.accounts.list()
.then(function(request,response){
console.log(response.text);
if (response.items && response.items.length) {
// Get the first Google Analytics account.
var firstAccountId = response.items[0].id;
console.log(firstAccountId);
}
else {
console.log('No accounts found for this user.');
}
});
});
}
render(){
return(
<div>
<Dashboard />
<label for="Company_Name">Company Name:</label>
<input type="text" id="Company_Name" />
<label for="URL">Website URL:</label>
<input type="text" id="pwd" />
<label for="Connect">Analytics</label>
<button onClick={ () => this.queryAccounts() }>Connect to Analytics</button>
<button type="submit" class="btn btn-default">Continue</button>
<MetaTags>
<meta name="google-signin-client_id" content="1133221725-0tke0s65lfp9pq2gk219d2h1mtccn5v7.apps.googleusercontent.com" />
<meta name="google-signin-scope" content="https://www.googleapis.com/auth/analytics.readonly https://www.googleapis.com/auth/analytics https://www.googleapis.com/auth/analytics.edit " />
</MetaTags>
</div>
);
}
}
export default connect()(AddWebsite);
單擊按鈕時,會調用queryAccounts()
,它應該提供分析帳戶列表,並且成功調用 api。響應是在 Chrome 開發工具中設置的,但是當我登錄控制台時,它返回未定義。
在 chrome Dev Tools 中設置的響應是:
{"kind":"analytics#accounts","username":"avisha1997.ab@gmail.com","totalResults":1,"startIndex":1,"itemsPerPage":1000,"items":[{"id":"106194863","kind":"analytics#account","selfLink":"https://www.googleapis.com/analytics/v3/management/accounts/106194863","name":"My Life Jobs","permissions":{"effective":[]},"created":"2017-09-11T06:39:57.512Z","updated":"2017-09-11T06:42:02.258Z","childLink":{"type":"analytics#webproperties","href":"https://www.googleapis.com/analytics/v3/management/accounts/106194863/webproperties"}}]}
queryAccounts() {
console.log('inside queryAccounts');
gapi.client.load('analytics', 'v3').then(function() {
gapi.client.analytics.management.accounts.list()
.then(function(response){
console.log(response); // try consoling and you will we able to see the response. response is the first arguments not the request
if (response.items && response.items.length) {
// Get the first Google Analytics account.
var firstAccountId = response.items[0].id;
console.log(firstAccountId);
}
else {
console.log('No accounts found for this user.');
}
});
徹底參考這個鏈接Link
響應中沒有文本屬性。 你可以 console.log response.items.length。
console.log(response.items.length);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.