节点js使用另一个文件V2.0中的函数从JSON文件打印信息

时间:2017-10-16 23:12:31

标签: node.js

这是我之前提到的另一个问题Node.js Printing info from JSON file using a function from another JS file

的延续

在我之前的问题中,我在从我的数据服务.js文件调用函数时遇到了问题,该文件打印了我的JSON数组中的所有项目,并且已经解决了,但是现在我正在努力做类似的打印工作我通过url指定的JSON数组中的员工。例如,http://localhost:8080/employeesstatus=5将仅打印状态为5的员工,但不会打印任何内容

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 object = require('./data-service');

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

//app.get('/employees', function(req,res){
 //   return object.getAllEmployees()
 //   .then((response) => res.send(response))
//});  //QUESION FROM PREVIOUS POST WHICH WAS RESOLVED


app.get('/employees:?status=:value', function(req,res){
return object.getEmployeesByStatus(req.params.value)
  .then((response) => res.send(response));

});

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(){

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

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

var getEmployeesByStatus = function (status){
return check().then(function(x){
var employees2 = JSON.parse(employees);
for (var i = 0; i<employees2.length; i++){
    if (employees2[i].status == status){
        return console.log(employees2[i]);
      }
  }       
  }).catch(function(){
      console.log("no results returned");
  })
}

module.exports.getAllEmployees = getAllEmployees;
module.exports.getEmployeesByStatus = getEmployeesByStatus;

有问题的2个功能

app.get('/employees:?status=:value', function(req,res){
return object.getEmployeesByStatus(req.params.value)
  .then((response) => res.send(response));

});

var getEmployeesByStatus = function (status){
return check().then(function(x){
var employees2 = JSON.parse(employees);
for (var i = 0; i<employees2.length; i++){
    if (employees2[i].status == status){
        return employees2[i];
    }
}       
}).catch(function(){
    console.log("no results returned");
})
}

1 个答案:

答案 0 :(得分:0)

1)您应该使用以下

替换/ employees route
app.get('/employees/status=:value', function(req,res){
 return object.getEmployeesByStatus(req.params.value)
 .then((response) => res.send(response));
});

您可以使用http://localhost:8080/employees/status=5

进行访问

2)返回employees2 [i]而不是console.log(employees2 [i])。