获取200状态代码,但响应中的响应未定义

时间:2018-02-09 05:26:33

标签: reactjs google-analytics-api

/*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开发工具中设置,但当我登录控制台时,它返回undefined。

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"}}]}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

回复中没有文字道具。你可以使用console.log response.items.length。

console.log(response.items.length);