你好,我有一个问题
我有一个模块并创建了对象“事件”然后我将此对象导出到我的应用程序中的其他模块。
但是在我的一个模块中它不起作用。我要保存的变量值为“undefined”
我不明白为什么
这里是我的开始文件[app.js]
console.log('[Server] run...');
require('./SCAN/Scan');
require('./eventHandler/eventHandler');
require('./database/database');
require('./socket-server/socket-server');
require('./scanner/scanner');
在此文件中我创建了事件对象
require('./eventHandler/eventHandler');
来自eventHandler的内容
console.log('[Server] eventHandler.js loaded!');
let io = require('./../socket-server/socket-server').io_ex;
let Event = new ( require('events').EventEmitter)();
exports.EventEmitter_ex = Event;
文件套接字服务器:
console.log('[Server] socket-server.js loaded!');
let Scan = require('./../SCAN/Scan').Scan_ex;
// create Server and listen on Port 8181
let app = require('express')();
let server = require('http').Server(app).listen(8181);
let io = require('socket.io')(server);
let EventEmitter = require('./../eventHandler/eventHandler').EventEmitter_ex;
io.on('connection', function (socket) {
console.log('[Socket] User Connected!');
if (!Scan.j > 0) {
return
}
// do if user connect
socket.on('fetchData', function () {
socket.emit('getFetch', Scan);
socket.emit('newStatus', Scan.status);
console.log('[Socket] getFetch Send!');
});
socket.on('disconnect', function () {
console.log('[Socket] User Disconnect');
});
//User Registierung
socket.on('register_user', function (userData) {
let regExp = /^[A-Za-z0-9]*[A-Za-z0-9]$/;
console.log('[APP - Registrierung] - Registrierung empfangen!');
if (!regExp.test(userData.username) && !regExp.test(userData.passwort)) {
console.log('[APP - Registrierung] - Fehlerhafte Daten von UserData');
return
}
let name = userData.username.toString();
let passwort = userData.passwort.toString();
let deviceID = userData.uuid.toString();
let user = {
u_name: name,
u_password: passwort,
u_uuid: deviceID
};
});
});
//----------------------------
exports.io_ex = io;
为什么导入未定义:
let EventEmitter = require('./../eventHandler/eventHandler').EventEmitter_ex;
答案 0 :(得分:1)
因为您需要 eventHandler 模块中的套接字服务器模块。这意味着 socket-server 模块在 eventHandler 模块设置其 EventEmitter_ex 导出的值之前初始化。