【问题标题】:Uncaught (in promise): TypeError: Cannot read property 'people' of undefined未捕获(承诺):TypeError:无法读取未定义的属性“人”
【发布时间】:2019-05-16 11:01:51
【问题描述】:

我被困住了。我正在尝试在 Angular 中创建 Google People API 身份验证服务。

我在 Chrome 控制台中遇到错误:

Uncaught (in promise): TypeError: Cannot read property 'people' of undefined

谁能帮我理解为什么?

declare var gapi: any;

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  user$: Observable<firebase.User>;
  peopleItems: any[];

  constructor(public afAuth: AngularFireAuth) {
    this.initClient();
    this.user$ = afAuth.authState;
  }

  initClient() {
    gapi.load('client', () => {

      gapi.client.init({
        apiKey: 'myapikey',
        clientId: '',
        discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/people/v1/rest"],
        scope: 'https://www.googleapis.com/auth/contacts'
      })
    });
  }

  async getPeople() {
    const mypeople = await gapi.client.people.people.connections.list({
      resourceName: 'people/me',
      personFields: 'names,emailAddresses',
    })
    this.peopleItems = mypeople.people.people.list;
  }
}

【问题讨论】:

  • 可能是因为mypeople.people 或mypeople 未定义。
  • 谢谢。我将 this.peopleItems = mypeople.people.people.list 更改为 mypeople.result.connections。
  • @GregHarner 请标记答案
  • 我不确定“标记答案”是什么意思。我确实继续并单击橙色 ^ 图标。这是一个答案吗?
  • 好的,我从帮助中看到没有标记答案。但是,我没有看到允许我标记答案的按钮。它藏在哪里?

标签: angular google-api


【解决方案1】:

我从 Google 快速入门文档中提取了 “mypeople.people.people.list”。但是,快速入门是针对 javascript 的,我使用的是 Angular。 @Bergi 用他的评论指出了我正确的方向。我使用 chrome developer tools (F12) 控制台选项卡在 sources 选项卡中查找代码行并添加了断点。那时我不仅能够清楚地看到 people 是未定义的。它还向我展示了从对象获取数据的正确路径。

我希望还有其他一些初学者可以从我的回答中受益。

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2020-09-24
    • 1970-01-01
    • 2020-03-24
    • 2021-05-14
    相关资源
    最近更新 更多