Node.js使用另一个JS文件中的函数从JSON文件打印信息

时间:2021-12-16 16:02:50

My function in my DATA-SERVICE.JS prints all the info from my JSON file and so I'm trying to call/print this function from my SERVER.JS file by using module.exports and even though it is printing in the integrated terminal, I get a blank screen when I search localhost:8080/employees Tried to simply console.log it but no luck

我的DATA-SERVICE.JS中的函数打印了我的JSON文件中的所有信息,所以我试图通过使用module.exports从我的SERVER.JS文件调用/打印此函数,即使它在集成终端中打印当我搜索localhost时,我得到一个空白屏幕:8080 /员工试图简单地控制它。但它没有运气

//SERVER.JS

//SERVER.JS

var HTTP_PORT = process.env.PORT || 8080;
var express = require('express');
var data = require('./data-service');
var fs = require('fs');
var app = express();
var getAllEmployees = require('./data-service');

console.log("Express http server listening on 8080");

app.get('/employees', function(req,res){

  res.end(JSON.stringify(getAllEmployees));

});

app.listen(8080, function(){
});
app.use(express.static('public'));

DATA-SERVICE.JS

DATA-SERVICE.JS

var employees = [];
var departments = [];
var error = 0;
var fs = require("fs");

function initialize(){

employees = fs.readFileSync("./data/employees.json", 'utf8', function(err, data){
    if(err){
        error = 1;
    }
    employees = JSON.parse(data);

});


departments = fs.readFileSync("./data/department.json", 'utf8', 
function(err, data){
    if(err){
        error = 1;
    }
    departments = JSON.parse(data);

});
}

function check() {
return new Promise(function(resolve,reject){

    if (error === 0){
        resolve("Success");

    }
    else if(error === 1){
       reject("unable to read file");
    }
})     
};

var getAllEmployees = function(){

check().then(function(x){
      console.log(x);
      console.log(employees);

  }).catch(function(x){
      console.log("No results returned");
  });
}
module.exports = getAllEmployees;


JSON
[
{
"employeeNum": 1,
"firstName": "Foster",
"last_name": "Thorburn",
"email": "fthorburn0@myCompany.com",
"SSN": "935-74-9919",
"addressStreet": "8 Arapahoe Park",
"addresCity": "New York",
"addressState": "NY",
"addressPostal": "20719",
"maritalStatus": "single",
"isManager": true,
"employeeManagerNum": null,
"status": "Full Time",
"department": 2,
"hireDate": "4/30/2014"
},
{
"employeeNum": 2,
"firstName": "Emmy",
"last_name": "Trehearne",
"email": "etrehearne1@myCompany.com",
"SSN": "906-43-6273",
"addressStreet": "66965 Shelley Circle",
"addresCity": "New York",
"addressState": "NY",
"addressPostal": "33605",
"maritalStatus": "single",
"isManager": true,
"employeeManagerNum": null,
"status": "Full Time",
"department": 2,
"hireDate": "6/25/2016"
},
{
"employeeNum": 3,
"firstName": "Zonnya",
"last_name": "Laytham",
"email": "zlaytham2@myCompany.com",
"SSN": "985-80-6616",
"addressStreet": "24665 Scoville Parkway",
"addresCity": "New York",
"addressState": "NY",
"addressPostal": "14609",
"maritalStatus": "single",
"isManager": true,
"employeeManagerNum": null,
"status": "Full Time",
"department": 2,
"hireDate": "2/1/2009"
},  //etc

1 个解决方案

#1


0  

You should return the promise in getAllEmployees function.

您应该在getAllEmployees函数中返回promise。

var getAllEmployees = function(){

return check().then(function(x){
  console.log(x);
  console.log(employees);
  return employees;

}).catch(function(x){
  console.log("No results returned");
});
}

Now, replace /employees route as below

现在,替换/员工路线如下

app.get('/employees', function(req,res){
return getAllEmployees()
    .then((response) => res.send(response))
});

#1


0  

You should return the promise in getAllEmployees function.

您应该在getAllEmployees函数中返回promise。

var getAllEmployees = function(){

return check().then(function(x){
  console.log(x);
  console.log(employees);
  return employees;

}).catch(function(x){
  console.log("No results returned");
});
}

Now, replace /employees route as below

现在,替换/员工路线如下

app.get('/employees', function(req,res){
return getAllEmployees()
    .then((response) => res.send(response))
});