Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

259
Vistas
La actualización del archivo Yaml a través del método Patch no funciona

Tengo un archivo Yaml ( layouts.yaml ) de este formato desde el que quiero realizar operaciones crudas a través de REST Api:

 Layouts: - Name: Default Layout LayoutId : 1 ConfiguredSegments: LiveA : Height : 100 Id : LiveA Ref1A : Height : 100 Id : Ref1A

La función de mi controlador para actualizar un diseño basado en la identificación del diseño (probé 2 formas que no funcionarán):

1ra manera: //Esto no parece funcionar

 const raw = fs.readFileSync("layouts.yaml", 'utf8'); const layoutData = YAML.load(raw); //function to update specific layout based on LayoutId export const updateSpecificLayout = (req, res)=>{ const { id } = req.params; const { ConfiguredSegments } = req.body; const getLayoutList = JSON.parse(JSON.stringify(layoutData)); getLayoutList.forEach(element => {if(element.LayoutId == id) element.ConfiguredSegments = ConfiguredSegments }); let yaml = YAML.dump(getLayoutList); fs.writeFileSync("layouts.yaml", yaml, function (err,file){ if(err) throw err; console.log(`Layout with the id:${id} has been updated`); }) }

2nd way://Esto no parece funcionar tan bien

 const raw = fs.readFileSync("layouts.yaml", 'utf8'); const layoutData = YAML.load(raw); //function to update specific layout based on LayoutId export const updateSpecificLayout = (req, res)=>{ const { id } = req.params; const { ConfiguredSegments } = req.body; const getLayout = JSON.parse(JSON.stringify(layoutData)); const foundLayout = getLayout.Layouts.find((layout) => layout.LayoutId == id); if(ConfiguredSegments)foundLayout.ConfiguredSegments = ConfiguredSegments; console.log(`Layout with the id:${id} has been updated`); }

A través de Postman, estoy probando mi API con una solicitud de parche con el siguiente cuerpo:

 { "ConfiguredSegments": { "Ref2A": { "Height": 100, "Id": "LiveA" }, "Ref3A": { "Height": 100, "Id": "Ref1A" } } } But the yaml file is not getting updated.Any other ways to achieve this ?
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Puedes intentar usar este método. Defina una función que podrá encontrar y reemplazar el objeto que está buscando. Su función de controlador:

 export const updateSpecificLayout = (req, res)=>{ const { id } = req.params; const { ConfiguredSegments } = req.body; const getLayoutList = JSON.parse(JSON.stringify(layoutData)); const layoutToBeUpdated = getLayoutList.Layouts.find((layout) => layout.LayoutId == id ); findAndReplace(getLayoutList.Layouts,layoutToBeUpdated.ConfiguredSegments,ConfiguredSegments) let yaml = YAML.dump(getLayoutList); fs.writeFileSync("layouts.yaml", yaml, function (err,file){ if(err) throw err; console.log(`Layout with the id:${id} has been updated`); }) }

La función auxiliar que puede encontrar y reemplazar los datos.

 // Helper function to update layout data function findAndReplace(object, value, replacevalue) { for (var x in object) { if (object.hasOwnProperty(x)) { if (typeof object[x] == 'object') { findAndReplace(object[x], value, replacevalue); } if (object[x] == value) { object["ConfiguredSegments"] = replacevalue; break; } } } }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda