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

时间:2022-08-04 16:03:19

This is a continuation from another question I asked earlier Node.js Printing info from JSON file using a function from another JS file

这是我之前提出的另一个问题的延续Node.js使用另一个JS文件中的函数从JSON文件打印信息

In my previous question I had problems in calling a function from my data-service.js file that printed all the items in my JSON array, and had it resolved, but now I'm struggling in doing something similar in printing only the employees from my JSON array that I specify through the url. For example http://localhost:8080/employeesstatus=5 would print only the employee with a status of 5 however nothing is getting printed

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

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 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

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;

The 2 functions in question

这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 个解决方案

#1


0  

1) You should replace /employees route with the following

1)您应该用以下内容替换/员工路线

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

You are able to access using http://localhost:8080/employees/status=5

您可以使用http:// localhost:8080 / employees / status = 5进行访问

2) Return employees2[i] instead of console.log(employees2[i]).

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

#1


0  

1) You should replace /employees route with the following

1)您应该用以下内容替换/员工路线

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

You are able to access using http://localhost:8080/employees/status=5

您可以使用http:// localhost:8080 / employees / status = 5进行访问

2) Return employees2[i] instead of console.log(employees2[i]).

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