• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

MMM-Domoticz Cannot read property '0' of undefined

Scheduled Pinned Locked Moved Unsolved Troubleshooting
15 Posts 3 Posters 3.3k Views 3 Watching
Loading More Posts
  • 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.
  • M Offline
    MisterT
    last edited by Sep 7, 2019, 12:59 PM

    Hi everybody
    I update my magic mirror from 2.6 to 2.8 and also npm to the latest version and now i have error on console (ctrl+ maj+i) with MMM-Domoticz:

    domoticz.js:150 Uncaught TypeError: Cannot read property ‘0’ of undefined
    at Class.processJson (domoticz.js:150)
    at XMLHttpRequest.domoRequest.onreadystatechange (domoticz.js:128)

    you see below the domoticz.js short program:

    line 115: updateDomo: function() {
    		var i = 0;
    		for (var c in this.sensors) {
    			//console.log("this is c: " + c);
    			var sensor = this.sensors[c];
    			var url = this.config.apiBase + ":" + this.config.apiPort + "/json.htm?type=devices&rid="  + sensor.idx;
    			var self = this;
    
    			var domoRequest = new XMLHttpRequest();
    			domoRequest.open("GET", url, true);
    			domoRequest.onreadystatechange = function() {
    				if (this.readyState === 4) {
    					if (this.status === 200) {
    						line 128: self.processJson(JSON.parse(this.response));
    						//console.log("Loaded data");
    					} else {
    						Log.error(self.name + ": Could not load data.");
    						//console.log("Did not load data");
    					}
    				}
    			};
    			domoRequest.send();
    			i++;
    		}
    	},
    
    	processJson: function(data) {
    		//console.log("****Parsing data: " + c + " " + data.result[0].Name);
    		if (!data) {
    			// Did not receive usable new data.
    			// Maybe this needs a better check?
    			return;
    		}
    		for (var c in this.sensors) {
    			var sensor = this.sensors[c];
    			line 150: if(sensor.idx == data.result[0].idx){
    				this.sensors[c].sname = data.result[0].Name;
    				this.sensors[c].status = data.result[0].Data;
    				this.sensors[c].type = data.result[0].Type;
    			}
    		}
    

    and also i have also a problem with console.log (to mm2.6 also) the error message is:

    console.log is not a function.

    I post this because i’m noob in js script. My intenet searching for this problem is a failure.

    Can anyboby help me please?
    thanls for next answers

    S 1 Reply Last reply Sep 7, 2019, 1:08 PM Reply Quote 0
    • S Offline
      sdetweil @MisterT
      last edited by Sep 7, 2019, 1:08 PM

      @MisterT said in MMM-Domoticz Cannot read property '0' of undefined:

      console.log is not a function.

      in the module.js, logging is done with the Log function, not console…

      so, change any console.log to Log.log

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      1 Reply Last reply Reply Quote 0
      • S Offline
        sdetweil
        last edited by Sep 7, 2019, 1:11 PM

        @MisterT said in MMM-Domoticz Cannot read property '0' of undefined:

          	var sensor = this.sensors[c];
        

        this is not set correctly if called in the domoRequest.onreadystatechange = function() {}
        u should be using self… ‘this’ is context sensitive, means ‘inside this function’

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        1 Reply Last reply Reply Quote 0
        • M Offline
          MisterT
          last edited by Sep 8, 2019, 2:02 PM

          @sdetweil said in MMM-Domoticz Cannot read property '0' of undefined:

          this is not set correctly if called in the domoRequest.onreadystatechange = function() {}
          u should be using self… ‘this’ is context sensitive, means ‘inside this function’

          hi @sdetweil, i replace conole.log by Log.log and now i have error in the console

          Could not load data.
          refered to line 131.

          Sorry but i don’t unerstand your last post about this and self. Can you show me the good syntax or do you need the entire program?

          thanks for your help

          S 1 Reply Last reply Sep 9, 2019, 1:51 AM Reply Quote 0
          • S Offline
            sdetweil @MisterT
            last edited by Sep 9, 2019, 1:51 AM

            @MisterT change this line

            Log.error(self.name + ": Could not load data.");
            

            to this

            Log.error(self.name + ": Could not load data. request status="+this.status);
            

            this will show the error return code on the api url request for the data

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            1 Reply Last reply Reply Quote 0
            • M Offline
              MisterT
              last edited by Sep 10, 2019, 5:04 PM

              hi @sdetweil , sorry for this late answer, i’m very busy at this moment

              I applied your programe line and now, in the console i have:

              :8080/modules/fabio/domoticz//domoticz.js:142 Uncaught TypeError: Cannot read property ‘0’ of undefined
              at Class.processJson (:8080/modules/fabio/domoticz//domoticz.js:142)
              at XMLHttpRequest.domoRequest.onreadystatechange (:8080/modules/fabio/domoticz//domoticz.js:128)

              and this:

              domoticz.js:142 Uncaught TypeError: Cannot read property ‘0’ of undefined
              at Class.processJson (domoticz.js:142)
              at XMLHttpRequest.domoRequest.onreadystatechange (domoticz.js:128)
              processJson @ domoticz.js:142
              domoRequest.onreadystatechange @ domoticz.js:128
              XMLHttpRequest.send (async)
              updateDomo @ domoticz.js:136
              (anonymous) @ domoticz.js:170

              and the js script below

              /* global Module */
              
              /* Magic Mirror
               * Module: Domoticz
               *
               * By Mathias Arvidsson
               */
              
              Module.register("domoticz",{
              
              	defaults: {
              		units: config.units,
              		updateInterval: 50,
              		animationSpeed: 0,
              		timeFormat: config.timeFormat,
              		lang: config.language,
              
              		initialLoadDelay: 0,
              		retryDelay: 2500,
              
              		apiBase: "http://1xx.1xx.x.xx", // IP domoticz 
              		apiPort: "xxxx",				 // Port
              		sensors: [
              			{
              				idx: "1",
              				symbolon: "fa fa-user",
              				symboloff: "fa fa-user-o",
              				hiddenon: false,
              				hiddenoff: false,
              				customTitle: "",
              			},
              		],
              	},
              
              	firstEvent: false,
              	getStyles: function() {
              	    return ['font-awesome.css'];
              	},
              	// Define required scripts.
              	getScripts: function() {
              		return ["moment.js"];
              	},
              
              
              
              	// Define start sequence.
              	start: function() {
              		Log.info("Starting module: " + this.name);
              
              		// Set locale.
              		moment.locale(config.language);
              
              		this.loaded = false;
              		this.status1 = false;
              		this.title = "Loading...";
              		this.scheduleUpdate(this.config.initialLoadDelay);
              		this.sensors = [];
              		for (var c in this.config.sensors) {
              			var sensor = this.config.sensors[c];
              			var newSensor = {idx:sensor.idx, symbolon:sensor.symbolon, symboloff:sensor.symboloff, hiddenon:sensor.hiddenon, hiddenoff:sensor.hiddenoff, customTitle:sensor.customTitle, status:"", sname:"",type:""};
              			Log.log(sensor.idx);
              			this.sensors.push(newSensor);
              		}
              Log.log(this.sensors);
              	},
              
              
              
              	// Override dom generator.
              	getDom: function() {
              		var wrapper = document.createElement("div");
              
              		if (!this.loaded) {
              			wrapper.innerHTML = "Loading...";
              			wrapper.className = "dimmed light small";
              			return wrapper;
              		}
              		var tableWrap = document.createElement("table");
              		tableWrap.className = "small";
              
              		for (var c in this.sensors) {
              			var sensor = this.sensors[c];
              			if((sensor.status=="On" && sensor.hiddenon)||(sensor.status=="Off" && sensor.hiddenoff)) continue;
              			var sensorWrapper = document.createElement("tr");
              			sensorWrapper.className = "normal";
              
              			var symbolTD = document.createElement('td');
              			symbolTD.className = "symbol";
              			var symbol = document.createElement('i');
              			var symbolClass = sensor.symboloff
              			if(sensor.status=="On") symbolClass = sensor.symbolon
              			symbol.className = symbolClass;
              			symbolTD.appendChild(symbol);
              			sensorWrapper.appendChild(symbolTD);
              
              			var titleTD = document.createElement('td');
              			titleTD.className = "title bright";
              			if(sensor.status=="Off") titleTD.className = "title light";
              			titleTD.innerHTML = sensor.sname;
              			if(typeof sensor.customTitle !== 'undefined') titleTD.innerHTML = sensor.customTitle;
              			sensorWrapper.appendChild(titleTD);
              
              			var statusTD = document.createElement('td');
              			statusTD.className = "time light";
              			statusTD.innerHTML = sensor.status;
              			sensorWrapper.appendChild(statusTD);
              
              			tableWrap.appendChild(sensorWrapper);
              		}
              		wrapper.appendChild(tableWrap);
              		return wrapper;
              	},
              
              
              	updateDomo: function() {
              		var i = 0;
              		for (var c in this.sensors) {
              			Log.log("this is c: " + c);
              			var sensor = this.sensors[c];
              			var url = this.config.apiBase + ":" + this.config.apiPort + "/json.htm?type=devices&rid="  + sensor.idx;
              			var self = this;
              
              			var domoRequest = new XMLHttpRequest();
              			domoRequest.open("GET", url, true);
              			domoRequest.onreadystatechange = function() {
              				if (this.readyState === 4) {
              					if (this.status === 200) {
              						self.processJson(JSON.parse(this.response));
              						Log.log("Loaded data");
              					} else {
              						Log.error(self.name + ": Could not load data. request status="+this.status);
              						Log.log("Did not load data");
              					}
              				}
              			};
              			domoRequest.send();
              			i++;
              		}
              	},
              
              	processJson: function(data) {
              		Log.log("****Parsing data: " + c + " " + data.result[0].Name);
              		if (!data) {
              			// Did not receive usable new data.
              			// Maybe this needs a better check?
              			return;
              		}
              		for (var c in this.sensors) {
              			var sensor = this.sensors[c];
              			if(sensor.idx == data.result[0].idx){
              				this.sensors[c].sname = data.result[0].Name;
              				this.sensors[c].status = data.result[0].Data;
              				this.sensors[c].type = data.result[0].Type;
              			}
              		}
              		
              		this.loaded = true;
              		this.updateDom(this.config.animationSpeed);
              	},
              
              	scheduleUpdate: function(delay) {
              		Log.log("Updating..");
              		var nextLoad = this.config.updateInterval;
              		if (typeof delay !== "undefined" && delay >= 0) {
              			nextLoad = delay;
              		}
              
              		var self = this;
              		setInterval(function() {
              			self.updateDomo();
              		}, 500 );
              	}
              });
              

              for your information, the module seems to work but i have this error.

              Have you a solution?
              thanks a lot

              S N 2 Replies Last reply Sep 10, 2019, 5:49 PM Reply Quote 0
              • S Offline
                sdetweil @MisterT
                last edited by Sep 10, 2019, 5:49 PM

                @MisterT could you email the module config (from config.js) to my email same userid as here on gmail…

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                S M 2 Replies Last reply Sep 10, 2019, 5:50 PM Reply Quote 0
                • S Offline
                  sdetweil @sdetweil
                  last edited by Sep 10, 2019, 5:50 PM

                  @sdetweil there is also a newer module (2 months) same name from another user

                  https://github.com/SpoturDeal/MMM-Domoticz

                  this module (M-Arvidsson) hasn’t been updated in 3 yrs

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  1 Reply Last reply Reply Quote 0
                  • N Offline
                    Nilnik Project Sponsor @MisterT
                    last edited by Sep 10, 2019, 8:13 PM

                    @MisterT I have the same module domoticz (M.Arvidsson) and it works fine for me. I can seei n your domoticz.js that ievery row with log.log … , I have console.log. Maybe thats the problem.

                    1 Reply Last reply Reply Quote 0
                    • M Offline
                      MisterT @sdetweil
                      last edited by Sep 11, 2019, 6:15 PM

                      @sdetweil i send you a personal mail with my domoticz module and config.js.
                      Thanks for your help

                      N S 2 Replies Last reply Sep 11, 2019, 7:44 PM Reply Quote 0
                      • 1
                      • 2
                      • 1 / 2
                      1 / 2
                      • First post
                        7/15
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy