首页 > 解决方案 > 将嵌套的 JSON 数据输出到节点 JS 变量

问题描述

美好的一天,我如何从 Json 获取数据并使其成为 Nodejs 上的变量?我如何计算时间,我要使用什么语法?我是NodeJS的新手,在此先感谢

应用程序.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的菜鸟,帮我一把?先感谢您

标签: node.jsjsonexpress

解决方案


推荐阅读