[英]How to delete an item in a json file via http delete request in nodeJS?
So Basically, i have a JSON file which consists of user and group data. 所以基本上,我有一个由用户和组数据组成的JSON文件。 I want to delete a particular group.
我想删除一个特定的组。 This is what my JSON file looks like: authdata.json:
这是我的JSON文件的样子:authdata.json:
[{
"name": "Allan",
"role": ["Group Admin", "Super Admin"],
"group": ["Cool-Group", "ss"]
}, {
"name": "super",
"group": ["Nerd Group"],
"role": ["Super Admin"]
}, {
"name": "Terry",
"role": ["Group Admin"],
"group": ["Cool-Group"]
}, {
"name": "Kaitlyn",
"role": ["Group Admin"],
"group": ["Nerd-Group"]
}, {
"name": "Alex",
"role": ["Group Admin"],
"group": ["Cool-Group"]
}]
I'm just confused on how to handle a http delete request in nodeJS? 我只是对如何处理nodeJS中的http删除请求感到困惑? this how my angular component is sending the request to the server: remove.component.ts:
这就是我的角度组件将请求发送到服务器的方式:remove.component.ts:
RemoveGroup() {
this.httpService.delete < any > (this.apiURL + 'deletegroup', {
group: this.groups
}).subscribe(
data => {
if (data['success'] == true) {
alert(data.group + " is removed");
} else {
alert("No groups found");
}
},
(err: HttpErrorResponse) => {
console.log(err.message);
}
);
}
This is the server side on NodeJS (reading the json file, assigning the data to a variable, trying to delete the group (which is not working for me) and writting back to the JSON file): deletegroup.js: 这是NodeJS的服务器端(读取json文件,将数据分配给变量,尝试删除组(对我不起作用)并写回JSON文件):deletegroup.js:
app.delete('/api/deletegroup', (req, res) => {
// localhost:3000/api/auth?username=Terry
var groups = req.body.group;
var userObj;
fs.readFile('authdata.json', 'utf8', function(err, data) {
if (err) {
console.log(err);
//Some error happended opening the file. No Success
res.send({
'group': '',
'success': false
});
} else {
userObj = JSON.parse(data);
for (let i = 0; i < userObj.length; i++) {
for (let j = 0; i < userObj.length; j++) {
if (userObj[i].group[j] == groups) {
userObj.splice(userObj.indexOf(groups), 1);
//find first instance of user name and success
}
}
}
var newdata = JSON.stringify(userObj);
fs.writeFile('authdata.json', newdata, 'utf-8', function(err) {
if (err) throw err;
//Send response that registration was successfull.
res.send({
'group': groups,
'success': true
});
});
//no username was found that matched
}
});
});
I assume that the problem is not with HTTP DELETE request. 我认为问题不在于HTTP DELETE请求。 The concern is with how to remove a child node.
关注的是如何删除子节点。 See the below code snippet.
请参见以下代码段。 You can pass the groups as an array to the deleteGroup function and see the result.
您可以将组作为数组传递给deleteGroup函数,并查看结果。
var data = [{
"name": "Allan",
"role": ["Group Admin", "Super Admin"],
"group": ["Cool-Group", "ss"]
}, {
"name": "Terry",
"role": ["Group Admin"],
"group": ["Cool-Group"]
}];
function deleteGroup(groupArray) {
groupArray.map((needle)=>{
data.map((userObj) => {
if(userObj.group) {
userObj.group.map((groupName, index)=>{
if (groupName == needle){
userObj.group.splice(index)
}
});
} else {
console.log("No group present for this user.")
}
});
});
return data
}
//uncomment below line & run in console to see the results
//console.log(deleteGroup(["Cool-Group"]))
Try out directly in Jsbin - https://jsbin.com/pejidag/1/edit?js,console 直接在Jsbin中试用-https://jsbin.com/pejidag/1/edit ? js,控制台
Happy coding! 编码愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.