Ich habe ein Modul, das eine Funktion auf einem Array von Daten ausführt, imitiert. Wenn ich die Funktion ausführe, erhalte ich die Ergebnisse in meinem Server, aber sie scheinen nicht in meiner Variablen gespeichert zu sein, die unbestimmt zurückkommt. Kann mir jemand sagen, warum mein Array undefiniert zurückkommt, wenn ich das Array auf meinem Server sehen kann?Array zeigt undefiniert in Variable
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");
});
Welche Art von Wert erhalten Sie von diesem Anruf. foo.addMitarbeiter ('William', 'Smith'); – abdulbarik
Vom Aufruf foo.lookupByLastName ('Smith'). Ich bekomme ausgewählte Elemente aus einem Array. Es sieht so aus [{id: 1, firstName: 'John', lastName: 'Doe'}]. Der foo.addEmployee ('William', 'Smith') fügt einfach ein Element zu der Datenaufnahme aus dem importierten Modul hinzu. –