我正在尝试创建 API,出于客户端目的,我正在使用节点邮递员。在删除方法中,客户端能够删除 json 对象,但它没有反映在 json 文件中。
const express = require("express");
const user = require("./data.json");
const fs = require("fs");
const app = express();
app.route("/user/:id").delete((req,res)=>{
const body = req.params.id;
console.log(eval(body));
user.map((obj)=>{
if(obj.id==eval(body)){
console.log("matched");
console.log(user.indexOf(obj));
delete user[user.indexOf(obj)];
console.log("successfully removed=>"+obj);
}
})
})
JSON 文件数据:
[
{
"id": 1,
"first_name": "Lilah",
"last_name": "Smitherham",
"email": "[email protected]",
"gender": "Female",
"title": "Accountant I"
},
{
"id": 2,
"first_name": "Donaugh",
"last_name": "Zanni",
"email": "[email protected]",
"gender": "Male",
"title": "Quality Control Specialist"
},
{
"id": 3,
"first_name": "Neila",
"last_name": "Hillyatt",
"email": "[email protected]",
"gender": "Female",
"title": "Registered Nurse"
},
{
"id": 4,
"first_name": "Granny",
"last_name": "Baszkiewicz",
"email": "[email protected]",
"gender": "Male",
"title": "Teacher"
},
{
"id": 5,
"first_name": "Reinaldos",
"last_name": "Christophe",
"email": "[email protected]",
"gender": "Male",
"title": "Recruiter"
},
{
"id": 6,
"first_name": "Zebedee",
"last_name": "Bulford",
"email": "[email protected]",
"gender": "Male",
"title": "Programmer Analyst II"
},
{
"id": 7,
"first_name": "Maxie",
"last_name": "Rudinger",
"email": "[email protected]",
"gender": "Female",
"title": "Geologist I"
},
{
"id": 8,
"first_name": "Vivi",
"last_name": "Hiscocks",
"email": "[email protected]",
"gender": "Female",
"title": "Software Consultant"
},
{
"id": 9,
"first_name": "Adlai",
"last_name": "Charer",
"email": "[email protected]",
"gender": "Male",
"title": "Senior Cost Accountant"
},
{
"id": 10,
"first_name": "Loydie",
"last_name": "Stribling",
"email": "[email protected]",
"gender": "Male",
"title": "VP Product Management"
},
{
"id": 11,
"first_name": "Farly",
"last_name": "Champness",
"email": "[email protected]",
"gender": "Male",
"title": "Operator"
},
{
"id": 12,
"first_name": "Jasper",
"last_name": "Dollimore",
"email": "[email protected]",
"gender": "Male",
"title": "Media Manager IV"
}
//so on 1000 users.
]
错在哪里?
由于您已将 JSON 数据加载到内存数组中,因此您可以在其上使用各种数组函数来搜索和修改它。当您想从中删除特定项目时,可以使用
Array.splice()
函数,您可以在此处阅读更多相关信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/参考/全局对象/数组/拼接
该函数获取您要删除的元素索引,然后是您要从该位置删除的元素数量。在这种情况下,您只想删除一个。
您可以使用
Array.findIndex()
功能找到该用户的索引。这需要一个回调来匹配每条记录,直到找到你想要的记录并返回true
。然后它知道记录的索引并将其传回给您供您使用。
我建议对您的 DELETE 端点使用以下代码:
app.route("/user/:id").delete((req, res) => {
const userId = Number(req.params.id);
console.log(userId);
// handle case where user ID is not a number
if(isNaN(userId)) {
res.sendStatus(400);
return;
}
const userIndex = user.findIndex(userRecord => userRecord.id === userId);
// handle case where user does not exist - userIndex will be -1 if the ID was not matched
if(userIndex < 0) {
res.sendStatus(404);
return;
}
// splice returns an array of deleted elements
const deletedUsers = user.splice(userIndex, 1);
console.log("successfully removed=>", deletedUsers);
res.sendStatus(200);
});
我已经在回复中提到了这一点,但我删除了对
eval()
的调用,因为这会给您的应用程序带来严重的安全风险。由于用户可以将任意字符串写入 URL 的该部分,因此他们可能会将任意 JavaScript 代码传递到您的应用程序中,然后由 eval()
函数运行。这意味着攻击者可以在您的服务器上运行他们自己的代码,从而导致服务器受到损害。
由于您所需要做的就是将字符串转换为数字,因此您可以使用
Number()
构造函数,这是进行此转换的便捷方法。如果您传入的字符串不是实数,它也不会崩溃,而只是为您提供一个特殊的 JavaScript 值 NaN
,它代表“不是数字”。您可以使用内置功能 isNaN()
在继续之前快速检查。
还要记住的一件事是,这只会编辑内存中的用户列表,而不会编辑 JSON 文件。如果您希望这些更改保留在 JSON 文件中,则每次更新或删除用户时都必须将 JSON 写回到文件中。