我一直处于等待状态仅在异步函数错误中有效,但是该函数是异步的,我没有看到什么?

时间:2020-04-22 16:10:58

标签: javascript node.js google-sheets-api

好的,我正在使用此google spreadsheet package

的示例代码

这是我在index.js中的代码:

const { GoogleSpreadsheet } = require('google-spreadsheet');

// spreadsheet key is the long id in the sheets URL
const doc = new GoogleSpreadsheet('1eSiMGlk0Gkb-HoIExoRDOffUw5ZLyMyMolXEZuHBrsk');


// OR load directly from json file if not in secure environment
await doc.useServiceAccountAuth(require('./privatekey.json'));


await doc.loadInfo(); // loads document properties and worksheets
console.log(doc.title);
await doc.updateProperties({ title: 'renamed doc' });

const sheet = doc.sheetsByIndex[0]; // or use doc.sheetsById[id]
console.log(sheet.title);
console.log(sheet.rowCount);

// adding / removing sheets
const newSheet = await doc.addSheet({ title: 'hot new sheet!' });
await newSheet.delete();

现在我得到了错误

await doc.useServiceAccountAuth(require('./privatekey.json'));
^^^^^
SyntaxError: await is only valid in async function

如果我们转到该函数,则显然是异步的

async useServiceAccountAuth(creds) {
    this.jwtClient = new JWT({
      email: creds.client_email,
      key: creds.private_key,
      scopes: GOOGLE_AUTH_SCOPES,
    });
    await this.renewJwtAuth();
  }

我忽略或做错了什么?

0 个答案:

没有答案