编程语言
首页 > 编程语言> > javascript-“未捕获的TypeError:无法读取未定义的属性’people’”

javascript-“未捕获的TypeError:无法读取未定义的属性’people’”

作者:互联网

我正在尝试使用Plus API登录用户.控制台中出现以下错误:

Uncaught TypeError:无法读取未定义的属性“ people”

这是我加载和获取个人资料信息的逻辑:

var firstName;
$(document).ready(function () {
  function loadGApi() {
    gapi.client.load('plus', 'v1');
  }
  $('#loaderror').hide();
});

function signInCallback(authResult) {
  if (authResult['status']['signed_in']) {
    // Update the app to reflect a signed in user
    // Hide the sign-in button now that the user is authorized, for example:
    $('#gConnect').hide();
    $('#authOps').show('slow');
    $('#profile').append(
      $('<p>Hello ' + getProfile(firstName) + '</p>'));
    console.log(authResult);

  } else {
    // Update the app to reflect a signed out user
    // Possible error values:
    //   "user_signed_out" - User is signed-out
    //   "access_denied" - User denied access to your app
    //   "immediate_failed" - Could not automatically log in the user
    console.log('Sign-in state: ' + authResult['error']);
  }
}

function getProfile(profile) {
  var request = gapi.client.plus.people.get({
    'userId': 'me'
  });
  if (profile == firstName) {
    request.execute(function (gprofile) {
      return gprofile.displayName;
    });
  }
}

这就是我加载脚本的方式:

(function() {
  var po = document.createElement('script');
  po.type = 'text/javascript'; po.async = true;
  po.src = 'https://plus.google.com/js/client:plusone.js?onload=loadGApi';
  var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(po, s);
})();

抱歉,这是一个菜鸟问题,但我希望了解更多有关Javascript和使用G API的信息!

解决方法:

一切正常,您可以异步注入Google javascript客户端.完成后,它会调用

gapi.client.load('plus', 'v1');

gapi.client.load需要3个参数,第3个参数是加载Google API时调用的回调.

由于您未指定回调,因此不会执行任何操作.

参见samples,它们定义了makeRequest回调:

gapi.client.load('urlshortener', 'v1', makeRequest);

function makeRequest() {
  var request = gapi.client.urlshortener.url.get({
    'shortUrl': 'http://goo.gl/fbsS'
  });
  request.execute(function(response) {
    appendResults(response.longUrl);
  });
}

因此,您想要执行以下操作:

gapi.client.load('plus', 'v1', onGapiLoaded);

function onGapiLoaded() {
  // now you can request Google+ api
}

更具体地说,Google+ API samples给出了onGapiLoaded回调中可以包含的内容的示例:

// Returns a request object which can be executed (as below) or batched
var request = gapi.client.METHOD_NAME(PARAMETERS_OBJECT);
request.execute(callback);

示例:您可以使用以下方法向Google API发送搜索请求:

var request = gapi.client.plus.activities.search({'query': 'Google+', 'orderBy': 'best'});
request.execute(function(resp) { console.log(resp); });

标签:google-plus,javascript
来源: https://codeday.me/bug/20191121/2053723.html