I want to start learning OOP and using fetch to GET information. But I am struggling at communicating between Classes. I am unable to GET the information into another Class for further processing.
My Code
Fetch.js
class Fetch {
constructor(data) {
this.data = data;
}
async fetchData(path) {
const res = await fetch(path)
const { status } = res;
if (status < 200 || status >= 300) {
return console.log("Oh-Oh! Seite konnte nicht gefunden werden: " + status)
}
this.data = res.text();
}
}
export default Fetch;
Day08.js
1 | import Fetch from "./Fetch.js";
2 |
3 | class Day08 extends Fetch {
4 | constructor() {
5 | super(data);
6 | this.doSomething();
7 | }
8 |
9 | doSomething() {
10 | console.log(this.data)
11 | }
12 | }
13 |
14 | new Day08();
Inside my Console, I then get, that data is not defined
in Day08 on line 6.
Question
So the question I have is, how can I use the data I got from the fetch?
I also tried writing my doSomething()
differently, like so:
doSomething() {
const fetching = new Fetch.fetchData("./Inputs/08-data.txt");
console.log(fetching)
}
But then I also get that data is not defined.
I don't know how to get the information and use it in a different class, I tried to look around online for an answer, but couldn't find one.
The input for fetching changes every time, to get different data. This is why I am trying to avoid making my Fetch Class with a static return.
If anyone could help me out with this, that would be great.