无法读取未定义的属性'emit'(重复)

[英]Cannot read property 'emit' of undefined (duplicate)


In my current project, I am reading data from TemperatureSensor through Node.js and pushing data on browser through web-socket. The Node.js code is as shown below:

在我目前的项目中,我通过Node.js从TemperatureSensor读取数据,并通过web-socket推送浏览器上的数据。 Node.js代码如下所示:

var http = require('http');
var fs = require('fs');
var port = 8686;
var path = require('path');
var mqtt = require('mqtt');
var sock;
var tempValue,humidityValue,sensorMeasurement;
var client = mqtt.connect('mqtt://test.mosquitto.org');
client.subscribe('sensorMeasurement');

client.on('message', function(topic, payload) {
if (topic.toString() == "sensorMeasurement") {  
sensorMeasurement = JSON.parse(payload);
tempValue=sensorMeasurement.tempValue;
console.log("temperature value : " + tempValue);
sock.emit('sensorData', { value : tempValue });
  }
});


function handleRequest(request, response){
var filePath = path.join(__dirname, 'index.html' );
var stat = fs.statSync(filePath);
response.writeHead(200, {
    'Content-Type': 'text/html',
    'Content-Length': stat.size
});
var readStream = fs.createReadStream(filePath);
readStream.pipe(response);      
}

var server = http.createServer(handleRequest);
var io = require('socket.io').listen(server);
server.listen(port, "0.0.0.0", function(){
console.log("Server listening on: http://localhost:%s", port);
});

io.sockets.on('connection', function (socket) {
sock = socket; 
});

When I run above code is displays following error:

当我在上面运行代码时显示以下错误:

C:\ABB Work\nodeworkspace\Smart Home App\DataVisualizer.js:18
 sock.emit('sensorData', { value : tempValue });
 TypeError: Cannot read property 'emit' of undefined
 at MqttClient.<anonymous> (C:\ABB Work\nodeworkspace\Smart Home App\DataVisualizer.js:18:7)
at emitThree (events.js:97:13)
at MqttClient.emit (events.js:175:7)
at MqttClient._handlePublish (C:\ABB Work\nodeworkspace\Smart Home App\node_modules\mqtt\lib\client.js:792:12)
at MqttClient._handlePacket (C:\ABB Work\nodeworkspace\Smart Home App\node_modules\mqtt\lib\client.js:282:12)
at process (C:\ABB Work\nodeworkspace\Smart Home App\node_modules\mqtt\lib\client.js:238:12)
at Writable.writable._write (C:\ABB Work\nodeworkspace\Smart Home App\node_modules\mqtt\lib\client.js:248:5)
at doWrite (C:\ABB Work\nodeworkspace\Smart Home App\node_modules\mqtt\node_modules\readable-stream\lib\_stream_writable.js:237:10)
at writeOrBuffer (C:\ABB Work\nodeworkspace\Smart Home App\node_modules\mqtt\node_modules\readable-stream\lib\_stream_writable.js:227:5)
at Writable.write (C:\ABB Work\nodeworkspace\Smart Home App\node_modules\mqtt\node_modules\readable-stream\lib\_stream_writable.js:194:11)

The sock is already defined as variable. Am I missing something ?

袜子已被定义为变量。我错过了什么吗?

1 个解决方案

#1


1  

it should be initialized with event Emitter

它应该用事件发射器初始化

var EventEmitter = require('events');
var sock = new EventEmitter();

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/09/07/481c14bc0d50bf18708185acb49039e2.html



 
  © 2014-2022 ITdaan.com