gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:59:37 27 4
gpt4 key购买 nike

当我在 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时,您不应传入回调。如果你想传递回调,你应该使用异步版本。读取文件。

关于javascript - 尝试打印特定属性时 Node.JS 数组变得未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46553786/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com