gpt4 book ai didi

javascript - NodeJS this.var 未定义

转载 作者:行者123 更新时间:2023-11-30 23:56:29 27 4
gpt4 key购买 nike

index.js

const request = require('request');

class ScheduleDownloader {

ScheduleDownloader(schoolId, username, schoolPassword) {
this.schoolId = schoolId;
this.username = username;
this.schoolPassword = schoolPassword;
}

getScheduleAtDate(dateString) {
console.log(`https://${this.username}:${this.schoolPassword}@www.myurl.de/${this.schoolId}/vplan/vdaten/VplanKl${dateString}.xml`)
/*request({url: `https://${this.username}:${this.schoolPassword}@www.myurl.de/${this.schoolId}/vplan/vdaten/VplanKl${dateString}.xml`}, (err, res, body) => {
if(err) console.error(err);
console.log(body);
});*/ // COMMENTED OUT FOR TESTING
};

module.exports = ScheduleDownloader;


var s = new ScheduleDownloader(10192417, 'schueler', 'S53998t');
s.getScheduleAtDate(20200317); // For testing in this class

输出

https://undefined:undefined@mnyurl.de/undefined/vplan/vdaten/VplanKl20200317.xml // undefined
https://www.myurl.de/10192417/vplan/vdaten/VplanKl20200317.xml // What it should be

如您所见,我正在尝试请求 xml 文件。我发现this.username等都是未定义的。我该如何解决?这又是什么原因呢? (抱歉,我是 JavaScript 新手)

最佳答案

在 javascript 构造函数中,称为构造函数,而不是类名,因此永远不会调用您的函数 ScheduleDownloader。将其更改为构造函数:一些阅读: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

关于javascript - NodeJS this.var 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61017383/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com