Home > Enterprise >  Missing data when recording model into JSON file
Missing data when recording model into JSON file

Time:11-07

I'm beginner and trying to save the model todoModel into data.json, but some elements (title, description) are not saved.

app.js

const http = require("http");
const todoRouter = require("./routes/todo.router");

const server = http.createServer(todoRouter);

const PORT = process.env.PORT || 3000;
server.listen(PORT, () =>
  console.log(`Server listening on http://localhost:${PORT}`)
);

todo.router.js

const url = require("url");
const todoController = require("../controllers/todo.controller");

const todoRouter = (req, res) => {
  const urlparse = url.parse(req.url, true);

  if (urlparse.pathname == "/todos" && req.method == "POST") {
    todoController.createTodo(req, res);
  }
};
module.exports = todoRouter;

todo.controller.js

const fs = require("fs");
class todoController {
 async createTodo(req, res) {
    req.on("data", (data) => {
      
      if (data) {
        todos.push(todoModel);

        fs.writeFile(
          "./data/data.json",
          JSON.stringify(todos, null, 2),
          (err) => {
            if (err) throw error;
          }
        );
      }
    });
  }}

todo.model.js

const { v4: uuidv4 } = require("uuid");
const fs = require("fs");

const data = fs.readFileSync("./data/data.json");
const jsondata = JSON.parse(data);
const title = jsondata.title;
const description = jsondata.description;

const todoModel ={
  id: uuidv4(),
  title,
  description,
  dateOfCreate: new Date(),
  lastModified: new Date(),
  check: new Boolean(false),
};

module.exports = todoModel;

Saved model todoModel in data.json looks like that:

[
  {
    "id": "cb996b22-d9d8-49ee-8e35-6f8bfc005268",
    "dateOfCreate": "2021-11-06T14:53:28.608Z",
    "lastModified": "2021-11-06T14:53:28.608Z",
    "check": false
  }
]

CodePudding user response:

Your values title and description don't have assigned a key in your todoModel. Just assign these values to proper keys in your object.

const todoModel ={
  id: uuidv4(),
  title: title, // add key for title
  description: description, // and here add key for description
  dateOfCreate: new Date(),
  lastModified: new Date(),
  check: new Boolean(false),
};

But the problem doesn't stop just here. Your problem is that you read the JSON file, load its content and then write the content back without any modifications. In the file todo.controller.js, you load the todoModel, which loads the content of the file. Then you add this content to todos array and write it right back to the file. You don't do any modification to todoModel, so what it does is pretty much nothing.

CodePudding user response:

Well i recommend to just export functions and constants and not variables or object witch can change. However i would rather use an function here

function getTodoModel() {
  const data = fs.readFileSync("./data/data.json");
  const jsondata = JSON.parse(data);
  const title = jsondata.title;
  const description = jsondata.description;

  return {
    id: uuidv4(),
    title,
    description,
    dateOfCreate: new Date(),
    lastModified: new Date(),
    check: new Boolean(false),
  }
}

module.exports = getTodoModel

Later you simply import your function and call it:

let todoModel = getTodoModel()

The variables will get evaluated

  • Related