我鑲嵌了一個模塊,該模塊對數據數組執行功能。當我運行這個函數時,我在我的服務器上得到了結果,但它們似乎沒有存儲在我的變量中,而這個變量未定義。有人能告訴我爲什麼我的數組在我的服務器中可以看到數組時爲何返回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");
});
你從這個調用得到什麼類型的值。 foo.addEmployee('William','Smith'); – abdulbarik
從調用foo.lookupByLastName('Smith')。我從數組中獲取選定的元素。它看起來像這樣[{id:1,firstName:'John',lastName:'Doe'}]。 foo.addEmployee('William','Smith')只是爲從導入的模塊獲取的數據添加一個元素。 –