I have a project that is working when using functions but when I convert it into a class it is giving errors when calling the same functions (now methods on the class).
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'getData' of undefined
at myClass.js:71:35
at FSReqCallback.oncomplete (fs.js:155:23)
Here is the function added as a method to the class:
class MyClass {
constructor() {
}
async myFunction(request, response, next) {
try {
// get file and write it to disk
fs.writeFile(file.originalname, file.buffer, async function(err) {
// error on the next line
var data = await this.getData();
And it's called like this:
const {MyClass} = require("./MyClass");
var myClass = new MyClass();
myClass.myFunction(request, response, next);
It worked when everything was a function in the same file.