[Script] Moon-phase appi form Rapid in .js script have some problems

To get the JSON file of the moon data it seem to me that it cant by the Logic Get card.

What I found was a .js script but there are some difficulties for Homey-script.

 1 const fetchMoonPhaseData = async () => {
 2   try {
 3        const response = await fetch("https://moon-phase.p.rapidapi.com/advanced", {
 4           "method": "GET",
 5            "headers": {
 6              "x-rapidapi-host": "moon-phase.p.rapidapi.com",
 7               "x-rapidapi-key": "xxxxxxxxx"
 8           }
 9       });
10
11       if (!response.ok) {
12            throw new Error(`HTTP error ${response.status}`);
13        }
14
15        const data = await response.json();
16        displayMoonPhase(data.phase_name);
17   } catch (error) {
18        console.error("Error fetching data:", error);
19        displayMoonPhase("Error fetching moon phase data");
20    }
21  };
22
23  const displayMoonPhase = (text) => {
24     const phaseTextElement = document.getElementById("phase-text");
25     phaseTextElement.innerText = text;
26  };
27
28  fetchMoonPhaseData();
29
30  // await tag('Moon data' , ?);

Line 30 is put by me for later getting the data out but how I don’t no jet.
The line numbers I put here myself so the lining is not correct.

In lines 1 and 23 the 1st error: =&gt. Changed in =>
Then the “;” in between the “=>” and “{” gives the next error.

Delete that “,” like a normal reaction but I’m tricky about it and don’t want to make problems on the api provider.

Further if some one can make it still working with the Logic Get card I prefer that.

At last I have that “;” deleted.

Outcome:
`
:white_check_mark: Script Success
:leftwards_arrow_with_hook: Returned: undefined

Error fetching data: ReferenceError: document is not defined
at displayMoonPhase (Moon data.js:24:30)
at fetchMoonPhaseData (Moon data.js:16:9)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)`

In line 23: const displayMoonPhase = (text) => {
3 dots under the 1st t of (text).

So I’m stuck again.

EDIT

Looking in my account I see that de data is going without error.
How to get them visible is still not happening.
Lines 22 - 29 are in remark.
Added this:

// await tag('Moon data' , ?);
console.log();
return(true);

This file seem to me part of an other program.
Looking to names with no var/const/let in front.

But the in line 3 const response I cant put it in the console.log(response)
———————————————————
:x: Script Error

:warning: ReferenceError: response is not defined
at Moon data.js:41:13
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

No idea for now to go further.

And Now for Something Completely Different


After dinner, 1 + 1 came together and I fount in my account an other possibility.
JavaScript with fetch. The above turned out to be for XMLHttpRequest

The only problem was the place of await tag(‘Moondata’,result); .
That had to be what higher in the lines then what I thought.

const url = 'https://moon-phase.p.rapidapi.com/advanced?lat=51.4768&lon=-0.0004';
const options = {
	method: 'GET',
	headers: {
		'x-rapidapi-key': 'xyx',
		'x-rapidapi-host': 'moon-phase.p.rapidapi.com'
	}
};

try {
	const response = await fetch(url, options);
	const result = await response.text();
	console.log(result);
	await tag('Moondata',result);
} catch (error) {
	console.error(error);
}
return(true);