在类javascript类构造函数中使用异步调用

在类javascript类构造函数中使用异步调用,javascript,class,javascript-objects,Javascript,Class,Javascript Objects,我正在尝试用Javascript创建一个类,基本上完成所有GoogleSheet函数。在编写任何函数之前,我在构造函数中调用authenticate API,但是在调用实际函数之前,身份验证没有完成。这是类文件中的代码 const { google } = require("googleapis") const authentication = require("./authentication"); class Sheets { constructor() { thi

我正在尝试用Javascript创建一个类,基本上完成所有GoogleSheet函数。在编写任何函数之前,我在构造函数中调用authenticate API,但是在调用实际函数之前,身份验证没有完成。这是类文件中的代码

const { google } = require("googleapis")
const authentication = require("./authentication");

class Sheets {
    constructor() {
        this.test = 'stackoverflow';
        authentication.authenticate().then((auth)=>{
            this.auth = auth;
        });
    }

    async writeToSheet(spreadsheetId, range, data) {
        if(typeof spreadsheetId != "undefined") {
            console.log(this.test)
            console.log('Got the spreadsheetId:' + spreadsheetId)
            console.log('Got the auth:' + this.auth)
            return;    
        }

        var sheets = google.sheets('v4');
        await sheets.spreadsheets.values.update({
            auth: this.auth,
            spreadsheetId: spreadsheetId,
            range: range, //Change Sheet1 if your worksheet's name is something else
            valueInputOption: "USER_ENTERED",
            resource: {
                values: data
            }
        }, (err, response) => {
            if (err) {
            console.log('The API returned an error: ' + err);
            return;
            } else {
                console.log("Data updated to the sheet successfully!");
            }
        });
    } 
}

module.exports = Sheets;
这就是我调用函数的方式

let sheets = await new Sheets();
sheets.writeToSheet(spreadSheetId, range, data);
当我像上面那样调用
writeToSheet
函数时,它总是像这样将auth打印为未定义

stackoverflow
Got the spreadsheetId:9ijWvEeWG7Oybv_TJi5AMkjl18dFHMFcBfSfkEG24-p7
Got the auth:undefined

那么,如何在调用类中的任何其他函数之前调用authenticate函数,并同步设置
this.auth
?在此方面的任何帮助都将不胜感激。

我认为您只需要创建一个额外的工作流类型函数来处理身份验证部分,然后将其传递给
writeToSheet
的逻辑;大概是这样的:

const { google } = require("googleapis")
const authentication = require("./authentication");

class Sheets {
    async authenticate() {
        const auth = await authentication.authenticate();
        this.auth = auth;
    }

    async writeToSheet(spreadsheetId, range, data) {
        await this.authenticate();
        await _writeToSheet(spreadsheetId, range, data);
    } 

    async _writeToSheet(spreadsheetId, range, data){
        if(typeof spreadsheetId != "undefined") {
            console.log('Got the spreadsheetId:' + spreadsheetId)
            console.log('Got the auth:' + this.auth)
            return;    
        }

        var sheets = google.sheets('v4');
        await sheets.spreadsheets.values.update({
            auth: this.auth,
            spreadsheetId: spreadsheetId,
            range: range, //Change Sheet1 if your worksheet's name is something else
            valueInputOption: "USER_ENTERED",
            resource: {
                values: data
            }
        }, (err, response) => {
            if (err) {
            console.log('The API returned an error: ' + err);
            return;
            } else {
                console.log("Data updated to the sheet successfully!");
            }
        });
    }
}

module.exports = Sheets;
这会稍微改变您对该函数的使用,如下所示:

let sheets = new Sheets();
await sheets.writeToSheet(spreadSheetId, range, data);

您可以将其中的一部分添加到
构造函数中,但请注意,您存储的是以前经过身份验证的类实例。如果您存储实例并稍后尝试使用现已过期的身份验证令牌重新使用它,则可能会出现问题。

您的类没有构造函数。@Ry-我没有在此处添加它,请立即检查。谢谢。我们不能像我在更新的代码中所显示的那样使用构造函数来实现这一点吗?因为我们不能为每个函数都创建这种函数,对吗?问题是只有在存储表单实例以供以后重用时才会出现。您可能会引入一个问题,即以后可以访问
工作表
,然后尝试使用可能不同的变量调用
writeToSheet
。如果身份验证在第二次调用之前过期,则下一次
writeToSheet
将自动失败。没有问题!很乐意帮忙。