我镶嵌了一个模块,该模块对数据数组执行功能。当我运行这个函数时,我在我的服务器上得到了结果,但它们似乎没有存储在我的变量中,而这个变量未定义。有人能告诉我为什么我的数组在我的服务器中可以看到数组时为何返回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')只是为从导入的模块获取的数据添加一个元素。 –