前提・実現したいこと
node.jsを使ってgoogle spreadsheetの値をとり、それを加工する
シートの読み取り部分クラス化 加工部分はメイン
発生している問題・エラーメッセージ
何が問題かわからない
(node:26599) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'context' of undefined at get (/home/pi/git/node_modules/googleapis/build/src/apis/sheets/v4.js:570:31) at Promise (internal/util.js:275:30) at new Promise (<anonymous>) at get (internal/util.js:274:12) at googleSheet.read (/home/pi/git/googleSheet.js:41:22) (node:26599) 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(). (rejection id: 2) (node:26599) [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.
該当のソースコード
javascript
1const fs = require('fs'); 2const { promisify } = require('util'); 3const { google } = require('googleapis'); 4const { OAuth2Client } = require('google-auth-library'); 5const sheets = google.sheets('v4'); 6 7const readFileAsync = promisify(fs.readFile); 8const ssValuesGetAsync = promisify(sheets.spreadsheets.values.get); 9 10 11module.exports = class googleSheet { 12 constructor() { 13 this.TOKEN_DIR = __dirname + "/token"; 14 this.TOKEN_PATH = this.TOKEN_DIR + '/sheets.googleapis.com-nodejs-quickstart.json'; 15 this.sheet_id = "*******************************************"; 16 } 17 18 async read() { 19 const content = await readFileAsync(__dirname + '/token/client_secret.json'); 20 const credentials = JSON.parse(content); 21 22 const clientSecret = credentials.installed.client_secret; 23 const clientId = credentials.installed.client_id; 24 const redirectUrl = credentials.installed.redirect_uris[0]; 25 const oauth2Client = new google.auth.OAuth2(clientId, clientSecret, redirectUrl); 26 const token = await readFileAsync(this.TOKEN_PATH); 27 oauth2Client.credentials = JSON.parse(token); 28 29 const apiOptions = { 30 auth: oauth2Client, 31 spreadsheetId: this.sheet_id, 32 range: 'A1:D36', 33 }; 34 35 return await ssValuesGetAsync(apiOptions, { context: google.auth }); 36 } 37}
###受け取り側
const googleSheet = require('./googleSheet'); const gsheet = new googleSheet(); ... let sheet = await gsheet.read();
試したこと
プロミス化している部分でのエラーというところまでは調べました。
補足情報(FW/ツールのバージョンなど)
raspberrypi3b+
Linux raspberrypi 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux
node v10.15.3
"@google-cloud/translate": "^3.0.1",
"dotenv": "^8.0.0",
"google-auth-library": "^3.1.2",
"googleapis": "^39.2.0",
"tmi.js": "^1.4.2",
"util": "^0.12.0"
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。