2017-06-14 68 views
0

你好,我有一個問題的Node.js模塊導入不會在一個文件中,但在其他文件的工作

我有一個模件和創建對象的「事件」,然後我導出此對象在我的應用程序的其他模塊。

但是在我的一個模塊中它不起作用。我要救它的值爲「未定義」

我不明白在至極的變量爲什麼

這裏我開始文件[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'); 

內容事件處理

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; 

回答

1

因爲你需要從插座服務器模塊eventHand模塊。這意味着套接字服務器模塊在事件處理程序模塊已經設置其值EventEmitter_ex導出的值之前初始化。

+0

我愛你thx !!!!!!!!! –

相關問題