我想向与我的Nodejs服务器连接的Android设备发送一些数据。但是当我发出事件时,出现“无法读取未定义的属性'发射'”错误。
服务器:
var nano = require('nano')('http://admin:1234@localhost:5984');
var gpsdata = nano.db.use('gpsdata');
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('new message', function incoming(message) {
gpsdata.list(function (err, body)
{
var current_number_of_tracks = body.total_rows + 1;
console.log(current_number_of_tracks);
gpsdata.insert({name: 'Route ' + current_number_of_tracks, message}, null, function(err, body) {
if (!err){
console.log(body);
}
});
});
console.log('received: %s', message);
});
socket.on('getdata', function(socket){
gpsdata.view('test', 'new-view', function(err, body){
if(!err)
{
body.rows.forEach(function(doc){
console.log(doc.value);
socket.emit('getRouteNames', doc.value); //Causes Error
});
}
});
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
server.listen(3000);
客户:
mSocket.on("getRouteNames", new Emitter.Listener() {
@Override
public void call(Object... args) {
routeNames.add(args);
}
});
TypeError:无法读取未定义的属性'emit'
C:\Users\Pavel\.atom\ws2\index.js:31
socket.emit('getRouteNames', doc.value).bind(socket);
^
TypeError: Cannot read property 'emit' of undefined
at C:\Users\Pavel\.atom\ws2\index.js:31:18
at Array.forEach (<anonymous>)
at C:\Users\Pavel\.atom\ws2\index.js:29:19
at Request._callback (C:\Users\Pavel\.atom\ws2\node_module\nano\lib\nano.js
:121:11)
at Request.self.callback (C:\Users\Pavel\.atom\ws2\node_modules\request\request.js:185:22)
at Request.emit (events.js:198:13)
at Request.<anonymous> (C:\Users\Pavel\.atom\ws2\node_modules\request\request.js:1161:10)
at Request.emit (events.js:198:13)
at IncomingMessage.<anonymous> (C:\Users\Pavel\.atom\ws2\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:286:20)
答案 0 :(得分:0)
好的,我只是覆盖Socket:
socket.on('getdata', function(socket)