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

Javascript 尝试打印特定属性时,Node.JS数组变得未定义,javascript,arrays,node.js,Javascript,Arrays,Node.js,当我在getEmployeesByStatus()函数中打印整个数组时,整个数组都会打印,但是当我尝试打印特定属性时,例如它们的状态(“全职或兼职”),它突然变得未定义,我不知道为什么。我的文件正在同步加载,所以这不是问题所在 var employees = []; var departments = []; var error = 0; var fs = require("fs"); function initialize(){ employees = fs.readFileSync(".

当我在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数组

[
{
"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条记录

这是因为此函数定义不正确:

function initialize(){

employees = fs.readFileSync("./data/employees.json", 'utf8', function(err, data){
if(err){
    error = 1;
}
employees = JSON.parse(data);
});
打印阵列时,请尝试同时打印
员工的类型

  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
时,不应传入回调。如果希望传入回调,则应使用异步版本viz。readFile。

这是因为此函数定义不正确:

function initialize(){

employees = fs.readFileSync("./data/employees.json", 'utf8', function(err, data){
if(err){
    error = 1;
}
employees = JSON.parse(data);
});
打印阵列时,请尝试同时打印
员工的类型

  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
时,不应传入回调。如果希望传入回调,则应使用异步版本viz。readFile。

您使用的是
fs.readFileSync()
,但将回调传递给它。这是不对的。没有传递到
fs.readFileSync()
fs.readFileSync()
的回调直接返回数据,并在出现错误时引发异常。您需要在返回值表单
fs.readFileSync()
上执行
JSON.parse()。这是不对的。没有传递到
fs.readFileSync()
fs.readFileSync()
的回调直接返回数据,并在出现错误时引发异常。您需要在返回值表单
fs.readFileSync()
上执行
JSON.parse()。