2016-09-15 112 views
-1

我鑲嵌了一個模塊,該模塊對數據數組執行功能。當我運行這個函數時,我在我的服務器上得到了結果,但它們似乎沒有存儲在我的變量中,而這個變量未定義。有人能告訴我爲什麼我的數組在我的服務器中可以看到數組時爲何返回undefined?顯示未定義變量的數組

var net = require('net'); 
var foo = require('./employeeModule'); 
var _ = require('underscore'); 
var colors = require('colors/safe'); 



var server = net.createServer(
    function(socket){ 
    console.log("Client connection..."); 

    socket.on('end', function(){ 
     console.log("Client disconnected..."); 
    }); 

// process data from client 
socket.on('data', function(data){ 
    var command = data.toString(); 
    var results={}; 
    console.log("Received Command: " +command); 
    if (command == "lookupByLastName Smith") 
     { 
     function lastName(results) 
     { 
      var results = foo.lookupByLastName('Smith'); 
      console.log('These are the results: '+ results) 
     } 
     lastName(); 
     } 

    else if (command == "addEmployee William Smith") 
     { 
     function addEmp(results) 
     { 
      var results = foo.addEmployee('William', 'Smith'); 
      console.log('These are the results: '+ results) 
     } 
     addEmp(); 
     } 

    else if (command == "lookupById 4") 
     { 
     function lookId(results) 
     { 
      var results = foo.lookupById(4); 
      console.log('These are the results: '+ Name) 
     } 
     lookId(); 
     } 

    else if (command == "bye") 
     client.end(); 

    else console.log(colors.green("**"+command+" Command not recognized!**"));  

    });  
}); 


//listent for client connections 
server.listen(1000, function(){ 
    console.log("Listening for client connections"); 
}); 
+0

你從這個調用得到什麼類型的值。 foo.addEmployee('William','Smith'); – abdulbarik

+0

從調用foo.lookupByLastName('Smith')。我從數組中獲取選定的元素。它看起來像這樣[{id:1,firstName:'John',lastName:'Doe'}]。 foo.addEmployee('William','Smith')只是爲從導入的模塊獲取的數據添加一個元素。 –

回答

0

首先,什麼是你的代碼錯誤:

  • var results={}:VaR的results不被使用。
  • 你有3個功能lastName,addEmplookId。所有被調用並且沒有任何通過,因此results var總是未定義的。
  • 在你定義的另一個函數var results中,因此,results參數(如覆蓋它)沒有做任何事情。
  • in lookId the var Name它是未定義的。
  • 我想你有參數硬編碼(command s,像William Smith,4Smith),因爲它是某種類型的「測試」/學習練習。我不建議你在回調函數中定義3個函數(因爲它們在每次被稱爲回調時都被定義)。

如果你想保留的結果做到這一點(以下您的編碼類型):

function addEmp(){ 
    var res = foo.addEmployee('William', 'Smith'); 
    console.log("There asre the results: " + res); 
    return res; 
} 
results = addEmp(); // now you set the value to results 

繼或多或少你會做什麼,我會做這樣的事情(注意,我改變一點點的編碼和邊界/輸入類型不受控制):

function lastName(surname){ 
    return foo.lookupByLastName(surname); 
} 
function addEmp(name, surname){ 
    return foo.addEmployee(name, surname); 
} 
// ... 

function socketData(data){ 
    var args = data.toString().split(' '); // assuming space as splitter 
    var results; 
    var command = args[0]; 
    switch(command){ 
    case 'lookupByLastName': 
    results = lastName(args[1]); 
    break; 
    case 'addEmp': 
    results = addEmp(args[1], args[2]); 
    break; 
    default: 
    // ... 
    } 
    // do something with results 
} 

var server = net.createServer(function(socket){ 
    // ... 
    socket.on('data', socketData); 
    // ... 
}); 
+0

我用於第一個解決方案,但我的res變量仍然顯示未定義。我重新定義了變量res ='This result';.這給我顯示了這個結果,但是當我嘗試將res存儲爲res = foo.lookupByLastName('Smith')時。它仍然返回undefined –

+0

如果我定義的'res'變量是未定義的,那僅僅是因爲你從'foo.addEmployee'函數返回未定義(或者什麼都沒有)。 – user3819881