Typeerror: Cannot read Property '1' of Undefined

Asked

Viewed 389 times

0

Good morning, I need your help. I am receiving data from the serial port "/dev/ttyUSB0", my goal" is to recover some information ( temperature and humidity ) and replace in the object enoceanRecords.

// Constants
const DEFAULT_SERIAL_PORT = "/dev/ttyUSB0";

// Dependancies
var fs = require('fs');
var vm = require('vm');
var events = require('events');
var cache = require('./plugins_cache.js');
var util = require('util');
var pk = require('node-enocean')();


// Type de données
var enoceanRecords = {
		Humidity: { 'value': '', 'type': 'TEXT', 'unit': '' },
		Temperature: { 'value': '', 'type': 'INTEGER', 'unit': 'A' }
	};
// Includes
vm.runInThisContext(fs.readFileSync(__dirname + "/" + "plugins_table.js"));
var table = new Table();

// Class
var PluginEnocean = function () {
    this.infos = { name: "enocean",
		 version: "0.0.1"
		 };
};

PluginEnocean.prototype.query = function () {
    return this.infos;
}

PluginEnocean.prototype.init = function (config) {
	 var options = config.get('plugin:enocean');
    var path = DEFAULT_SERIAL_PORT;

    if(options !== undefined) {
        if(options['path'] !== undefined) {
            path = options['path'];
        }
    }

    // Open serial port
    console.log(path);
    pk.listen(path);
	
	// Set table name
    table.setName(this.infos['name']);
	
	 // Collect data
    pk.on('data', function (data) {
			
			
			// =====================
			
			
			var temp = enoceanRecords['Temperature'];
			var humid = enoceanRecords['Humidity'] ;
			var valTemp = (data['values'])[1];
			var valHumid = (data['values'])[0];
			
			temp['value'] = valTemp['value'];
			temp['type']  = valTemp['type'];
			temp['unit']  = valTemp['unit'];
			
			humid['value'] = valHumid['value'];
			humid['type']  = valHumid['type'];
			humid['unit']  = valHumid['unit']; 
			
			//=====================
			
			var uuid = data['senderId'] ;
				
			for(key in enoceanRecords) {
					
			table.setRecord(key, enoceanRecords[key]);
			}
				
				
				
				
	// Feed data
			if(uuid !== undefined) { 
				table.setUuid(uuid);
				table.setRecord(key, enoceanRecords[key]);
				table.setStamp();
				cache.push(table);
			}
				});
		
	
};

PluginEnocean.prototype.run = function () {
};

PluginEnocean.prototype.exit = function () {
};

var enocean = module.exports = new PluginEnocean();

Error image

  • Good morning, I need your help! I am receiving data from the serial port "/dev/ttyUSB0", my goal" is to recover some information ( temperature and humidity ) and replace in the object "enoceanRecords" !!

  • Which is the line where that mistake is making?

  • @Miguel the error is in these two lines var valTemp = (date['values']); var valHumid = (date['values'])[0];

  • In the line before the error make a console.log(data['values']); and see what you print on the console, I think this is not getting the values as it should

  • @Miguel the program is working 100% when I test on firefox console for example, only it doesn’t work on "nodejs"... The behavior is not the same in the "nodejs" to which it is due ?!

  • Do you run Ode through the terminal? See what you print there

  • @Miguel, as I used to say, Script goes very well outside the "nodejs" but when I call : "sudo nodejs Scriptcolletiodedonnes.js " ça marche pas ! ( does not work )

  • 1

    Okay, I was just wondering what data['values'] store, I suspect that when the flame sudo Nod it will not have the value/values you are thinking

  • @Miguel this is + or - the format of the date I receive : data = { loadFromBuffer: [Function],
 senderId: '018a75ba',
 raw: '0088860a',
 sensor:
 { id: '018a75ba',
 eep: 'a5-04-01',
 manufacturer: 'ENOCEAN_GMBH',
 title: 'New Temperature and Humidity Sensor',
 desc: 'I\'m a new sensor...',
 eepFunc: 'Temperature and Humidity Sensor',
 eepType: 'Range 0°C to +40°C and 0% to 100%',
 last: [ [Object], [Object] ] },
 values:
 [ { type: 'humidity', unit: '%rF', value: 54.400000000000006 },
 { type: 'temperature', unit: '°C', value: 21.44 } ] };

  • I think I’ve already noticed the bug friend. paying on that showed => data for the 1st obj of the array data['values']: data.values[0].type = "humidity", data.values[0].value = 54.400, data.values[0].unit = "%rF"

  • @Miguel what’s the mistake bro? Exclaim better bro ?

  • If what is being stored in data That’s what you showed me, so I was having a hard time accessing the values I wanted

  • @Miguel what would be the correct procedure ? Merci !!

  • 1

    But in fact it seems to me that that’s what you’re doing in your code. Here you are: https://jsfiddle.net/9d23pqrb/ . Now the important thing is to know if data has even the data in this format that showed me

  • @Miguel thank you very much, It is worth noting that the data are not coming in the format that I was guessing ! I will take a look then give a sign ! ... Have a good afternoon @ Miguel... Merci Beaucoup !

  • Hi @Miguel once again I came by to thank you, I just solved the problem... Logic was right, the error was in the "format" of the data !! GRATEFUL AND ONCE AGAIN GRATEFUL !

  • Nothing. I’m glad you solved

  • @James in peace bro !

Show 13 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.