Reputation: 13
I want to use the google-spreadsheet API to fetch and write data to a sheet in my Discord bot (using discord.js). For some reason, I can't get it to work. This is my code:
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
)
})
})
}
This is the error I keep getting:
(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.
Upvotes: 0
Views: 748
Reputation: 13
I should actually use async/await for achieving this. My code has become:
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
})
Upvotes: 1