1

I have async function getTables() which using Google Sheet API to get google spreadsheet data. But on response it returns undefined value. this.getClient() returns OAuth2Client data. Could you check my async function, maybe it is written not properly?

async getTables(): Promise<any> {
    try {
      const sheets = google.sheets({
        version: "v4",
        auth: await this.getClient()
      });

      const res = await sheets.spreadsheets.values.get({
        spreadsheetId: "sheetId",
        range: "A1:B100"
      });

      return res;
      } catch (err) {
       throw new HttpException("The API returned an error: " + err, 500);
    }

This is getClient async function which authorize client.

async getClient(): Promise<OAuth2Client> {
    if (!this.oAuth2Client) {
      return await this.authorize();
    }

    return this.oAuth2Client;
  }


private async authorize() {
    const credentials = {
      access_token: this.auth.access_token,
      refresh_token: null
    };

    this.oAuth2Client = new google.auth.OAuth2(this.CLIENT_ID, this.clientSecret, this.redirectUrl);
    this.oAuth2Client.setCredentials(credentials);
    return this.oAuth2Client;
  }
  • What makes you think it returns undefined? Can you provide a minimal reproducible example, including the context in which you are calling `getTables()` and how you are checking it returns undefined? – Iamblichus Jan 18 '22 at 08:18
  • @Iamblichus I logged the response (res), and it shows undefined. ```@Get() async getSpreadSheet(): Promise { return await this.spreadService.getTables() }``` I am calling getTables() in controller. – Merei Temirkas Jan 18 '22 at 09:48
  • Can you provide the code related to `getClient`? If you remove the asynchronous parts, do you get the same behavior? – Iamblichus Jan 18 '22 at 12:59
  • @Iamblichus I updated my code above, where you can see getClient function. – Merei Temirkas Jan 19 '22 at 06:28
  • Thanks. As I mentioned before, did you try removing the asynchronous parts? Do you get the same behavior? – Iamblichus Jan 19 '22 at 13:16
  • @Iamblichus. If remove asynchronous parts before ```getClient```, then will error like "TypeError: authClient.request is not a function" . If remove from other parts, nothing will change. – Merei Temirkas Jan 20 '22 at 06:39

1 Answers1

0

It was my mistake, I just updated the version of Google API and everything is work, it returns array of data.