Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Missing data when recording model into JSON file

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
  }
]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!