Skip to content
  • Home
  • Recent
  • Tags
  • 0 Unread 0
  • Categories
  • Unreplied
  • Popular
  • GitHub
  • Docu
  • Hilfe
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Entwicklung
  4. Adapter-Entwicklung / getState / Callback Fehler

NEWS

  • Jahresrückblick 2025 – unser neuer Blogbeitrag ist online! ✨
    BluefoxB
    Bluefox
    16
    1
    1.6k

  • Neuer Blogbeitrag: Monatsrückblick - Dezember 2025 🎄
    BluefoxB
    Bluefox
    13
    1
    853

  • Weihnachtsangebot 2025! 🎄
    BluefoxB
    Bluefox
    25
    1
    2.1k

Adapter-Entwicklung / getState / Callback Fehler

Scheduled Pinned Locked Moved Entwicklung
6 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NemoN
    wrote on last edited by
    #1

    Hallo zusammen, ich kämpfe immer noch mit der Adapter-Entwicklung. Insbesondere async/await bzw. Promises machen mir zu schaffen. Ich habe hier folgenden Code Snippet:

    async onReady() {
    
    	await this.getState('lastUpdate', function (err, state) {
    		if (state != null) {
    			let now = new Date().getTime();
    			if (now < (state.ts + 10 * 1000)) {
    				this.log.warn("adapter ran less than 10 minutes earlier. Delaying.");
    				this.stop(); // stop adapter right here (on schedule mode)
    			}
    		}
    	});
    
    	this._queryServer();
    }
    

    Bei der Ausführung bekomme ich folgenden Fehler:

    TypeError: Cannot read property 'warn' of undefined
        at Socket.<anonymous> (/opt/iobroker/node_modules/iobroker.ryd/main.js:98:15)
        at Socket.onack (/opt/iobroker/node_modules/socket.io-client/lib/socket.js:312:9)
        at Socket.onpacket (/opt/iobroker/node_modules/socket.io-client/lib/socket.js:236:12)
        at Manager.<anonymous> (/opt/iobroker/node_modules/component-bind/index.js:21:15)
        at Manager.Emitter.emit (/opt/iobroker/node_modules/socket.io-client/node_modules/component-emitter/index.js:133:20)
        at Manager.ondecoded (/opt/iobroker/node_modules/socket.io-client/lib/manager.js:332:8)
        at Decoder.<anonymous> (/opt/iobroker/node_modules/component-bind/index.js:21:15)
        at Decoder.Emitter.emit (/opt/iobroker/node_modules/component-emitter/index.js:134:20)
        at Decoder.add (/opt/iobroker/node_modules/socket.io-parser/index.js:246:12)
        at Manager.ondata (/opt/iobroker/node_modules/socket.io-client/lib/manager.js:322:16)
    

    Wieso kann ich in dem getState Callback nicht auf die Adapter Funktionen (via this. ...) zugreifen? adapter.log.warn .... klappt auch nicht...

    foxriver76F 1 Reply Last reply
    0
    • N Offline
      N Offline
      NemoN
      wrote on last edited by
      #2

      Ich antworte mir mal selber:
      man muss beim adapter init noch folgendes definieren:

      Außerhalb der Klasse, globale Variable:

      var gthis;
      

      im onReady:

      	async onReady() {
      		gthis = this;
      

      im callback dann Zugriff via:

      if (now < (state.ts + 10 * 1000)) {
        gthis.log.warn("adapter ran less than 10 minutes earlier. Delaying.");
        gthis.stop(); // stop adapter right here (on schedule mode)
      }
      
      AlCalzoneA 1 Reply Last reply
      0
      • N NemoN

        Hallo zusammen, ich kämpfe immer noch mit der Adapter-Entwicklung. Insbesondere async/await bzw. Promises machen mir zu schaffen. Ich habe hier folgenden Code Snippet:

        async onReady() {
        
        	await this.getState('lastUpdate', function (err, state) {
        		if (state != null) {
        			let now = new Date().getTime();
        			if (now < (state.ts + 10 * 1000)) {
        				this.log.warn("adapter ran less than 10 minutes earlier. Delaying.");
        				this.stop(); // stop adapter right here (on schedule mode)
        			}
        		}
        	});
        
        	this._queryServer();
        }
        

        Bei der Ausführung bekomme ich folgenden Fehler:

        TypeError: Cannot read property 'warn' of undefined
            at Socket.<anonymous> (/opt/iobroker/node_modules/iobroker.ryd/main.js:98:15)
            at Socket.onack (/opt/iobroker/node_modules/socket.io-client/lib/socket.js:312:9)
            at Socket.onpacket (/opt/iobroker/node_modules/socket.io-client/lib/socket.js:236:12)
            at Manager.<anonymous> (/opt/iobroker/node_modules/component-bind/index.js:21:15)
            at Manager.Emitter.emit (/opt/iobroker/node_modules/socket.io-client/node_modules/component-emitter/index.js:133:20)
            at Manager.ondecoded (/opt/iobroker/node_modules/socket.io-client/lib/manager.js:332:8)
            at Decoder.<anonymous> (/opt/iobroker/node_modules/component-bind/index.js:21:15)
            at Decoder.Emitter.emit (/opt/iobroker/node_modules/component-emitter/index.js:134:20)
            at Decoder.add (/opt/iobroker/node_modules/socket.io-parser/index.js:246:12)
            at Manager.ondata (/opt/iobroker/node_modules/socket.io-client/lib/manager.js:322:16)
        

        Wieso kann ich in dem getState Callback nicht auf die Adapter Funktionen (via this. ...) zugreifen? adapter.log.warn .... klappt auch nicht...

        foxriver76F Offline
        foxriver76F Offline
        foxriver76
        Developer
        wrote on last edited by foxriver76
        #3

        @NemoN sagte in Adapter-Entwicklung / getState / Callback Fehler:

        Wieso kann ich in dem getState Callback nicht auf die Adapter Funktionen (via this. ...) zugreifen? adapter.log.warn .... klappt auch nicht...

        Weil du dann innerhalb einer Callback Funktion bist und der this-Kontext sich dann auf die Funktion bezieht. Was du da tust mit dem Mix aus Promises und Callbacks macht wenig Sinn.
        Promises (+ Async/Await was auf Promises basiert) funktioniert nur mit Methoden die auch Promises zurück liefern. getState tut dies nicht... was du brauchst ist getStateAsync zudem tut ein await Block die Asynchronität aus dem Code nehmen und wartet an der Stelle bis das Promsie returned wurde. D. h. es ersetzt Callbacks.

        Dein Code oben:

        async onReady() {
           try {
        	const state = await this.getStateAsync('lastUpdate');
        	const now = new Date().getTime();
        
        	if (now < (state.ts + 10 * 1000)) {
        	    this.log.warn("adapter ran less than 10 minutes earlier. Delaying.");
        	    this.stop(); // stop adapter right here (on schedule mode)
        	}
           } catch (e) {
                // handle error here
           }
        
           this._queryServer();
        }
        

        Videotutorials & mehr

        Hier könnt ihr mich unterstützen.

        1 Reply Last reply
        0
        • N NemoN

          Ich antworte mir mal selber:
          man muss beim adapter init noch folgendes definieren:

          Außerhalb der Klasse, globale Variable:

          var gthis;
          

          im onReady:

          	async onReady() {
          		gthis = this;
          

          im callback dann Zugriff via:

          if (now < (state.ts + 10 * 1000)) {
            gthis.log.warn("adapter ran less than 10 minutes earlier. Delaying.");
            gthis.stop(); // stop adapter right here (on schedule mode)
          }
          
          AlCalzoneA Offline
          AlCalzoneA Offline
          AlCalzone
          Developer
          wrote on last edited by AlCalzone
          #4

          @NemoN sagte in Adapter-Entwicklung / getState / Callback Fehler:

          Ich antworte mir mal selber:

          Wenn du unbedingt Callbacks brachst, verwende lieber Arrow-Funktionen anstatt this-Referenzen zu speichern:

          this.getState('lastUpdate', (err, state) => {
          

          anstatt

          this.getState('lastUpdate', function (err, state) {
          

          um dafür zu sorgen, dass this immer das richtige meint.

          Oder nimm direkt die Async-Versionen der Methoden wie @foxriver76 geschrieben hat. Da gibts das Problem gar nicht erst.

          Warum `sudo` böse ist: https://forum.iobroker.net/post/17109

          1 Reply Last reply
          0
          • foxriver76F Offline
            foxriver76F Offline
            foxriver76
            Developer
            wrote on last edited by
            #5

            Und wenn du tatsächlich callbacks verwendest wie von @AlCalzone gezeigt, brauchst du kein async vor der Klassenmethode und auch kein await bei deinen Methoden/Funktionsaufrufen.

            Videotutorials & mehr

            Hier könnt ihr mich unterstützen.

            1 Reply Last reply
            0
            • N Offline
              N Offline
              NemoN
              wrote on last edited by
              #6

              Danke für die Hinweise, langsam sehe ich klarer :v:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              Support us

              ioBroker
              Community Adapters
              Donate

              434

              Online

              32.6k

              Users

              82.1k

              Topics

              1.3m

              Posts
              Community
              Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen | Einwilligungseinstellungen
              ioBroker Community 2014-2025
              logo
              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Home
              • Recent
              • Tags
              • Unread 0
              • Categories
              • Unreplied
              • Popular
              • GitHub
              • Docu
              • Hilfe