Javascript &引用;未捕获类型错误:无法读取属性';人';“未定义”的定义;

Javascript &引用;未捕获类型错误:无法读取属性';人';“未定义”的定义;,javascript,google-plus,Javascript,Google Plus,我正在尝试使用Plus API登录用户。我在控制台中遇到以下错误: 未捕获类型错误:无法读取未定义的属性“people” 这是我加载和获取配置文件信息的逻辑: var firstName; $(document).ready(function () { function loadGApi() { gapi.client.load('plus', 'v1'); } $('#loaderror').hide(); }); function signInCallback(auth

我正在尝试使用Plus API登录用户。我在控制台中遇到以下错误:

未捕获类型错误:无法读取未定义的属性“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;
    });
  }
}


如果这是一个noob问题,我很抱歉,但我希望了解更多关于Javascript和使用G+API的信息

一切正常,您可以异步注入Google+javascript客户端。当这是donem时,它会呼叫

gapi.client.load('plus', 'v1');
但是需要3个参数,第3个是加载Google+API时调用的回调

因为您没有指定回调,所以没有执行任何操作

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

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

所以你想做一些类似的事情:

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

更具体地说,请给出一个示例,说明您可以在onGapiLoaded回调中使用什么:

gapi.client.load('urlshortener', 'v1', makeRequest);
// 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); });

正因为谷歌的API有时会令人痛苦,这里有一个使用承诺而不是回调的例子。(即使API名称无效,我也无法使用
load
方法触发错误,因此我省略了该函数。)

对于上下文,它位于单独的js文件中的Knockout.js ViewModel中。调用
ko.applyBindings
是在主HTML页面中进行的,使用
gapi
的依赖项注入将其保持在适当的范围内。
authenticate()
方法是从init函数中调用的,我将
client.load()
嵌套在回调中以处理“未验证”错误


那个错误是说
gapi.client.plus
不存在。@elclanrs是的,我意识到了,但我不明白为什么不加载它?谢谢你的帮助!声明后,永远不会调用
loadGApi()
。为什么要在jQuery'ready'中声明它?不确定,还没有使用G+API,但有消息告诉我这是一个异步问题,应该在某个地方使用回调。。。但我可能错了,也许其他人可以帮忙。@charlietfl-Hmmm。我想它不需要在“ready”中,但当Google API加载到这里时,它会被调用“po.src=”;”它作为URL参数作为文档传递。另外,我也很感谢您在文档中的帮助,它说回调是可选的:如果我确实使用回调,我将如何进行回调,以便我可以在多个函数中使用API,这些函数都以不同的方式调用回调确实是可选的:您没有指定回调,也没有抛出错误。加载Gapi后,您可以随心所欲地使用它。回调可能只是用您拥有的不同回调来装饰您的UI。
var request = gapi.client.plus.activities.search({'query': 'Google+', 'orderBy': 'best'});
request.execute(function(resp) { console.log(resp); });
function myVM(gapi) {
    var _gapi = gapi;
    var self = this;

    self.authenticate = function() {
        console.log("Loading Google API...");
        _gapi.load('auth', function(param) {
            console.log("Authenticating...");
            _gapi.auth.authorize({
                client_id: '1234',
                immediate: false, /* Needs to be false for initial authorization. Setting to true will prevent the subsequent popups. */
                response_type: 'token',
                scope: 'https://www.googleapis.com/auth/content'
            }, function (token) {
                if (token.access_token) {
                    console.log('Authorized!');
                    self.authorized(true);
                    _gapi.client.load('content', 'v2').then(function() {
                        // Do stuff
                    });
                } else {
                    console.log('Authorization failed!');
                    self.authorized(false);
                }
            });
        });
    }
}