javascript - 通过 Patch 方法更新 Yaml 文件不起作用
问题描述
我有一个这种格式的 Yaml 文件(layouts.yaml),我想通过 REST Api 执行 crud 操作:
Layouts:
-
Name: Default Layout
LayoutId : 1
ConfiguredSegments:
LiveA :
Height : 100
Id : LiveA
Ref1A :
Height : 100
Id : Ref1A
我的控制器根据布局 ID 更新布局的功能(我尝试了 2 种不起作用的方法):
第一种方式://这似乎不起作用
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`);
})
}
第二种方式://这似乎也不起作用
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`);
}
通过邮递员,我正在使用以下正文测试我的 api 补丁请求:
{
"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 ?
解决方案
您可以尝试使用此方法。定义一个能够找到并替换您正在寻找的对象的函数。您的控制器功能:
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`);
})
}
可以查找和替换数据的辅助函数。
// 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;
}
}
}
}
推荐阅读
- angular - Angular HttpClient 发布请求类型
- html - Bootstrap4 在列中获取卡片以水平和垂直拉伸和填充父容器
- javascript - 表之间的sequelize和query
- php - 试图从 $_SESSION foreach 循环 PHP 中获取索引
- java - 如何使用pdfbox获取pdf中选定文本的字体信息
- java - 如何使用 xpath 作为 sendkeys 从 selenium 上传只读文本文件将不起作用
- java - 二十一点代码中涉及 A 的逻辑错误
- jquery - 如何使用引导模型对话框将数据添加到数据表?
- xamarin - 如何在 MvvmCross 6.0 中指定使用来自共享项目的视图
- php - 虽然我使用 header('Location:test.php') 我必须重新加载页面