尝试打印特定属性时,Node.JS数组变为未定义

时间:2017-10-03 21:34:10

标签: javascript arrays node.js

当我在getEmployeesByStatus()函数中打印整个数组时,整个数组都会打印出来,但是当我尝试打印特定属性时,例如它们的状态(“全职或兼职”)时,它会突然变得不确定,我不会不知道为什么。我的文件同步加载,这不是问题

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");
      }
  })     
};

function getAllEmployees(){

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

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

function getEmployeesByStatus(status){
  check().then(function(){
  console.log(employees)   //Entire array prints
  console.log(employees[4].status)   //undefined

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

initialize();
getEmployeesByStatus("Full Time");

// EMPLOYEES.JSON ARRAY

[
{
"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"
},
{
"employeeNum": 4,
"firstName": "Asia",
"last_name": "Bollon",
"email": "abollon3@myCompany.com",
"SSN": "996-45-9010",
"addressStreet": "0464 Mitchell Road",
"addresCity": "New York",
"addressState": "NY",
"addressPostal": "50335",
"maritalStatus": "married",
"isManager": true,
"employeeManagerNum": null,
"status": "Full Time",
"department": 4,
"hireDate": "8/26/2004"
 },

大约200多条记录

1 个答案:

答案 0 :(得分:0)

这是因为此功能定义不正确:

function initialize(){

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

打印数组时,请尝试打印employees的类型。

  console.log(employees)   //Entire array prints
  console.log(typeof employees)   //prints string
  console.log(employees[4].status)   //undefined because the string's 4 character does not have a status property and is not an object per se

现在,定义你的函数初始化如下:

function initialize(){
  employees = JSON.parse(fs.readFileSync("./e.json", 'utf8'));
}

使用fs.readFileSync时,不应传入回调。如果要传入回调,则应使用异步版本即可。 READFILE。