NEWS
[Gelöst] SetState funktioniert nicht
-
Edit: Das Skript funktioniert auf einmal korrekt.
Hallo zusammen
Ich bin relativ neu mit JS unterwegs, daher bitte nicht böse sein, wenn die Antwort offensichtlich ist.
Ich habe ein Skript geschrieben, welches per API ein paar Daten abruft und diese in Objekte schreiben soll.
Soweit scheint das auch zu funktionieren und in den Logs sehe ich, dass die Werte "lastDownloads[0]" bis *lastDownloads[4]" korrekt gefüllt sind. Die Objekte erhalten aber keine neuen Werte und ich erhalte auch keine Fehlermeldung.Die Datenpunkte vom Typ state existieren ebenfalls.
Hat hier jemand eine Idee, wo das Problem liegen könnte?
const axios = require('axios'); const sonarrIP = '192.168.1.214:8093'; // IP und Port const apiKey = 'XYZ'; // API Key const options = { url: `http://${sonarrIP}/api/v3/history`, headers: { 'X-Api-Key': apiKey } }; axios(options) .then(response => { const records = response.data.records; const lastDownloads = []; for (let i = 0; i < records.length && i < 5; i++) { lastDownloads.push(records[i].sourceTitle); } //console.log(lastDownloads); console.log(lastDownloads[0]); console.log(lastDownloads[1]); console.log(lastDownloads[2]); console.log(lastDownloads[3]); console.log(lastDownloads[4]); // Schreibe die letzten 5 Downloads in die gewünschten Datenpunkte setState('0_userdata.0.Media.Sonarr.Letzte_Downloads.1', lastDownloads[0]); setState('0_userdata.0.Media.Sonarr.Letzte_Downloads.2', lastDownloads[1]); setState('0_userdata.0.Media.Sonarr.Letzte_Downloads.3', lastDownloads[2]); setState('0_userdata.0.Media.Sonarr.Letzte_Downloads.4', lastDownloads[3]); setState('0_userdata.0.Media.Sonarr.Letzte_Downloads.5', lastDownloads[4]); })
-
Auch wenn es bereits auf gelöst steht.
So mancher kennt den Stolperstein nicht.