通过 Patch 方法更新 Yaml 文件不起作用 [英] Updating Yaml File through Patch method not working
问题描述
我有一个这种格式的 Yaml 文件(layouts.yaml),我想通过 REST Api 从中执行 crud 操作:
I have a Yaml file(layouts.yaml) of this format from which i want to perform crud Operations through REST Api:
Layouts:
-
Name: Default Layout
LayoutId : 1
ConfiguredSegments:
LiveA :
Height : 100
Id : LiveA
Ref1A :
Height : 100
Id : Ref1A
我的控制器功能根据布局 ID 更新布局(我尝试了 2 种不起作用的方法):
My controller Function to update a layout based on layout Id(I tried 2 ways which wont work):
第一种方法://这似乎不起作用
1st way: //This does not seem to work
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://This does not seem to work as well
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`);
}
通过 Postman,我正在使用以下正文的补丁请求测试我的 api:
Through Postman i am testing my api with patch request with the following body:
{
"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 ?
推荐答案
您可以尝试使用此方法.定义一个函数,它将能够找到并替换您正在寻找的对象.您的控制器功能:
You can try using this method. Define a function which will be able to find and replace the object you are looking for. Your controller function:
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`);
})
}
可以查找和替换数据的辅助函数.
The helper function which can find and replace the data.
// 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;
}
}
}
}
这篇关于通过 Patch 方法更新 Yaml 文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!