【发布时间】:2016-09-15 23:43:53
【问题描述】:
我已经移植了一个对数据数组执行功能的模块。当我运行该函数时,我在我的服务器中获得了结果,但它们似乎没有存储在我的变量中,该变量返回未定义。当我在服务器中看到数组时,有人能告诉我为什么我的数组返回 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('威廉', '史密斯');
-
来自调用 foo.lookupByLastName('Smith')。我正在从数组中获取选定的元素。它看起来像这样 [{id: 1, firstName: 'John', lastName: 'Doe'}]。 foo.addEmployee('William', 'Smith') 只是将一个元素添加到从导入模块获取的数据中。