Avisha97
Avisha97

Reputation: 13

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

On the clicking of the button,queryAccounts() is called which should give the list of analytics accounts and the api call is successfully made.The response is set in Chrome Dev Tools but when I am logged in the console, it returns undefined.

The response set in chrome Dev Tools is:

{"kind":"analytics#accounts","username":"[email protected]","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"}}]}

Upvotes: 1

Views: 1972

Answers (2)

simbathesailor
simbathesailor

Reputation: 3687

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

Refer this link thoroughly Link

Upvotes: 1

vijayst
vijayst

Reputation: 21894

There is no text prop in the response. You could console.log response.items.length.

console.log(response.items.length);

Upvotes: 0

Related Questions