当我需要在TypeScript中为我的类属性分配值但出现错误时,我在做什么错: ERROR : uncaughtException: Cannot set property 'auth' of undefined
我需要auth
最初不保留任何信息的属性(1)来保存我在done
方法中分配的信息。但是,每当我想为该属性分配任何值时,在我的情况下它都不起作用。我已经检查了很多次,并且在done方法中的auth不是未定义的。它保存传递给它的信息,但无论如何都会弹出错误。
Setters
而且Getters
也没有帮助。
如果我需要在Angular中分配此值,那肯定可以,但是我想Class的使用会有些许差异。
import fs from 'fs';
import readline from "readline";
import { google } from "googleapis";
import path from 'path';
import { logger } from './winston';
class connectGoogleAPI {
private SCOPES = [
'https://www.googleapis.com/auth/gmail.readonly',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.send'
];
private readonly token_path = path.join(process.cwd(), 'token.json');
private readonly credential_path = path.join(process.cwd(), 'credentials.json');
private auth: any = 'value from auth';
set _auth(auth: any) {
this.auth = auth;
}
get _auth(): any {
return this.auth;
}
constructor() {
}
private done(_a: any) {
this.auth = _a;
// this.auth = (_a as object);
// console.log('done');
}
connect() {
// console.log('connect');
// Load client secrets from a local file.
fs.readFile(this.credential_path, { encoding: 'utf-8' }, (err: any, content: any) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Gmail API.
this.authorize(JSON.parse(content), this.done);
// authorize(JSON.parse(content), listLabels);
});
}
private authorize(credentials: any, callback: any) {
// console.log('authorize');
const { client_secret, client_id, redirect_uris } = credentials.web;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(this.token_path, { encoding: 'utf-8' }, (err: any, token: any) => {
if (err) return this.getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
logger.info(`Connected to Google API successfully`);
callback(oAuth2Client);
});
}
private getNewToken(oAuth2Client: any, callback: any) {
// console.log('getNewToken');
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: this.SCOPES,
});
logger.warn('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code: any) => {
rl.close();
oAuth2Client.getToken(code, (err: any, token: any) => {
if (err) return logger.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(this.token_path, JSON.stringify(token), (err: any) => {
if (err) return console.error(err);
logger.info('Token stored to', this.token_path);
});
return callback(oAuth2Client);
});
});
}
getAuth() {
if (this.auth === null || this.auth === undefined) {
logger.warn('Getting again auth - Google API');
this.connect();
}
return this.auth;
}
}
export const googleAPI = new connectGoogleAPI();
我将不胜感激任何帮助和提示。
1.我已经尝试在构造函数中分配一些信息,所以它不是空的,但它也能正常工作
起初我没有遇到问题,但问题在范围内。我调用了应该将值分配给属性的方法,作为回调函数。
我忘了把类绑定到它上)以防万一有人花很多时间编码并厌倦了看它...
connect() {
// Load client secrets from a local file.
fs.readFile(this.credential_path, { encoding: 'utf-8' }, (err: any, content: any) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Gmail API.
this.authorize(JSON.parse(content), this.done.bind(this));
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句