将嵌套的JSON数据输出到Node JS变量

时间:2018-12-06 17:43:50

标签: node.js json express

早上好,如何从Json获取数据并将其设置为Nodejs上的变量?以及如何计算时间,我将使用什么语法?我是NodeJS的新手,在此先感谢

  

App.js

这给我索引路径上的模块一个错误

const path = require('path');

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.set('view engine', 'ejs');
app.set('views', 'views');

const indexRoutes = require('./routes/index');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use(indexRoutes);


app.listen(8080);
  

控制器

const Schedule = require('../models/schedule');

exports.getSchedule = (req, res, next) => {
  Schedule.fetchAll(schedule => {
    res.render('index', {
      sched: schedule,
      pageTitle: 'Schedule',
      path: '/routes'
    });
  });
};
  

模型

const fs = require('fs');
const path = require('path');

const p = path.join(
  path.dirname(process.mainModule.filename),
  'data',
  'dbm_input.json'
);

const getScheduleFromFile = cb => {
  fs.readFile(p, (err, fileContent) => {
    if (err) {
      cb([]);
    } else {
      cb(JSON.parse(fileContent));
    }
  });
};

module.exports = class Schedule {
  constructor(employee_info,) {
    this.;
    this.;
    this.;
    this.;
  }

  static fetchAll(cb) {
    getScheduleFromFile(cb);
  }
};
  

路线

const path = require('path');

const express = require('express');

const ScheduleController = require('../controller/ScheduleController');

const router = express.Router();

router.get('/', ScheduleController.getSchedule);

module.exports = router;
  

JSON

{"652":
    {"employee_info":
            {"employee_name":""},
        "date_log":
            {
                "2017-12-31":
                {
                    "config":{
                        "shift":"R","hours_per_day":8,
                        "break_hours":1,"flexi_hours":0,
                        "grace_period":15
                    },
                    "log":{
                        "time_in":"2017-12-31 07:35:37",
                        "time_out":"2017-12-31 09:34:01",
                        "break_out":["2017-12-31 12:00:00"],
                        "break_in":["2017-12-31 13:00:00"],
                        "shift_in":"2017-12-31 16:00:00",
                        "shift_out":"2017-12-31 16:00:00",
                        "status":"present",
                        "holiday":"no",
                        "overtime":"no"}
                },
                "2017-12-29":
                    {
                        "config":{
                            "shift":"FL",
                            "hours_per_day":8,
                            "break_hours":1,
                            "flexi_hours":2,
                            "grace_period":0},
                        "log":{
                            "time_in":"2017-12-29 00:20:00",
                            "time_out":"2017-12-29 10:35:00",
                            "break_out":["2017-12-31 12:00:00"],
                            "break_in":["2017-12-31 13:00:00"],
                            "shift_in":"2017-12-29 16:00:00",
                            "shift_out":"2017-12-29 16:00:00",
                            "status":"present",
                            "holiday":"no",
                            "overtime":"no"
                    }
                },
                "2017-12-28":
                    {
                        "config":{
                            "shift":"R",
                            "hours_per_day":8,
                            "break_hours":1,
                            "flexi_hours":0,
                            "grace_period":0},
                        "log":{
                            "time_in":"2017-12-28 00:02:25",
                            "time_out":"2017-12-29 10:35:00",
                            "break_out":["2017-12-31 12:00:00"],
                            "break_in":["2017-12-31 13:00:00"],
                            "shift_in":"2017-12-28 16:00:00",
                            "shift_out":"2017-12-28 16:00:00",
                            "status":"present",
                            "holiday":"no",
                            "overtime":"no"
                            }}
                    }
                }
}

我已经搜索了很长时间,似乎我不知道应该搜索什么,因为我看不到解决方案。我只想将数据转换为变量,以及如何计算数据上给出的时间以及应将其放在哪里。以及当我计算数据时如何循环数据。我对nodejs真的很陌生,请帮我一下吗?预先谢谢你

0 个答案:

没有答案