未捕获(承诺):TypeError:无法读取未定义的属性“ people”

时间:2018-12-15 12:04:01

标签: angular google-api

我被困住了。我正在尝试在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;
  }
}

1 个答案:

答案 0 :(得分:0)

我从Google快速入门文档中摘录了“ mypeople.people.people.list” 。但是,快速入门是针对javascript的,而我使用的是Angular。 @Bergi用他的评论向我指出了正确的方向。我使用了 chrome开发者工具(F12)控制台标签在 sources 标签中找到代码行,并添加了一个断点。那时,我不仅能够清楚地看到是不确定的。它还向我展示了从对象获取数据的正确路径。

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