Как использовать google-таблицу в nodejs - PullRequest
0 голосов
/ 13 июля 2020

Я хочу использовать API электронных таблиц Google для извлечения и записи данных на лист в моем боте Discord (используя discord. js). По какой-то причине не могу заставить его работать. Это мой код:

const { GoogleSpreadsheet } = require('google-spreadsheet')
const creds = require('../service-account.json')

module.exports = {
    /...
    execute(message, args) {
        accessSpreadsheet()
    }
}

async function accessSpreadsheet() {
    const doc = new GoogleSpreadsheet('<id was here>')
    doc.useServiceAccountAuth(creds)
        .then(() => {
            doc.getInfo()
                .then(info => {
                    console.log(`Loaded doc: ` + info.title + ` by ` + info.author.email)
                    const sheet = info.worksheets[0]
                    console.log(
                        `sheet 1: ` + sheet.title + ` ` + sheet.rowCount + `x` + sheet.colCount
                    )
                })
        })
}

Я постоянно получаю эту ошибку:

 (node:4) UnhandledPromiseRejectionWarning: Error: Google API error - [404] Requested entity was not found.
     at createError (/app/node_modules/axios/lib/core/createError.js:16:15)
     at settle (/app/node_modules/axios/lib/core/settle.js:17:12)
     at IncomingMessage.handleStreamEnd (/app/node_modules/axios/lib/adapters/http.js:236:11)
     at IncomingMessage.emit (events.js:327:22)
     at endReadableNT (_stream_readable.js:1221:12)
     at processTicksAndRejections (internal/process/task_queues.js:84:21)
 (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
 (node:4) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1 Ответ

0 голосов
/ 15 июля 2020

Я должен использовать async / await для этого. Мой код стал:

async function accessSpreadsheet(message, args, msg) {
    const doc = new GoogleSpreadsheet('<id was here>')
    await doc.useServiceAccountAuth(creds)

    await doc.loadInfo()
    console.log(doc.title) //whatever you want to log goes here
})
...