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

196
Views
Cómo hacer un modelo en otro archivo

Soy principiante e intenté transferir el modelo a otro archivo, no me funcionó, sugiérame cómo hacerlo correctamente. La pregunta puede parecer tonta, pero si supiera la respuesta, no la haría.

archivo todo.controller.js

 const fs = require("fs"); const { v4: uuidv4 } = require("uuid"); const data = fs.readFileSync("./data/data.json"); let todos = JSON.parse(data); class todoController { async createTodo(req, res) { req.on("data", (data) => { const jsondata = JSON.parse(data); const title = jsondata.title; const description = jsondata.description; if ((title, description)) { todos.push({ id: uuidv4(), title, description, dateOfCreate: new Date(), lastModified: new Date(), check: new Boolean(false), }); fs.writeFile( "./data/data.json", JSON.stringify(todos, null, 2), (err) => { if (err) throw error; } ); } }); }}

archivo 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;

aquí está el archivo data.json data.json

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tiene dos problemas separados aquí, separando su código en un archivo diferente y también guardando o conservando esos datos en algún lugar, en este caso un archivo.

Tienes que crear algo como un modelo de datos y luego tienes que importarlo en tu otro código.

 // data.js export const get = async () => {} // we will implement this just now export const set = async (data) => {} // we will implement this just now ... // controller.js import {get, set} from './data.js' // import the methods we just created ... const createTodo = async (req, res) => { req.on("data", (data) => { // here you can use get() if you want to use the data set(JSON.stringify(data)) // send data to your data model } }

Entonces también tenemos que hacer algo con esos métodos.

 // data.js export const get = async () => { // may need to use JSON.parse here depending on how you'll use it return fs.readFile('./data.json') } export const set = async (data) => { fs.writeFile('data.json', JSON.stringify(data)) }

Entonces, la idea es tener un modelo responsable de administrar los datos, recuperarlos y guardarlos, luego importarlos y usar esos métodos en el controlador principal. El código anterior no es perfecto, es solo para mostrarle cómo pensar al respecto.

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!