MMM-Domoticz Cannot read property '0' of undefined



  • 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



  • @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



  • @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’



  • @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



  • @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



  • 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



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



  • @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


  • Project Sponsor

    @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.



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