Node.Js尝试通过另一个文件中的函数打印JSON数组

时间:2017-10-04 01:35:04

标签: javascript node.js

尝试打印到我的localhost所有管理器的JSON数组,但我遇到了一个空白屏幕。数组填充正确,问题(很可能)发生在server.js文件中。我不知道是什么。任何帮助赞赏。 编辑:在原帖中遗漏了一些代码的道歉

// server.js

var HTTP_PORT = process.env.PORT || 8080;
var express = require('express');
var data = require('./data-service');
var getManagers = require('./data-service');
var fs = require('fs');
var app = express();




app.get('/managers', function(req,res){
var responseObject = getManagers();
res.send(responseObject);
});

app.listen(8080, function(){
 });
app.use(express.static('public'));

//数据server.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 getManagers = function(){
check().then(function(){
    var employees2 = JSON.parse(employees);
    for (var i = 0; i < employees2.length; i++){
        if(employees2[i].isManager == true){
            console.log(employees2[i]);
        }
    }
}).catch(function(){
    console.log("no results found");
})
}

initialize();
module.exports = getManagers;

1 个答案:

答案 0 :(得分:0)

确保在类的构造函数中调用 <row> <panel> <title> Total packets received (in the last period) - sum(globalStatPktsRcvd) </title> <chart> <search> <query> index=snmp_stats sourcetype=snmp_collector_log device=* | timechart span=5m sum(globalStatPktsRcvd) by device </query> <earliest>-24h@h</earliest> <latest>now</latest> <sampleRatio>1</sampleRatio> </search> <option name="charting.chart">line</option> </chart> </panel> </row> ... <row> <panel> <title> Packets received per second† - sum(globalStatPktsRcvdRate) </title> <chart> <search> <query> index=snmp_stats sourcetype=snmp_collector_log device=* | timechart span=5m sum(globalStatPktsRcvdRate) by device </query> <earliest>-24h@h</earliest> <latest>now</latest> <sampleRatio>1</sampleRatio> </search> <option name="charting.chart">line</option> </chart> </panel> </row>

initialize

调用此模块应如下所示:

// Let's assume we create a Node module called
// 'myobject' from this.

function MyObject() {

  this.initialize();
}

MyObject.prototype = {
  initialize: function() {
     .... //use this.employees
  }

  check: function() {
      ...
  },
  getManager: function(){
      ...//use this.employees
  }
};

module.exports = MyObject;