node.js - 如何在 Node.js 中更新 JSON 对象值
问题描述
我的 Javascript 与以下 2 个 JSON 文件交互:
读者.json:
[
{
"readerID": 1,
"name": "Maria",
"weeklyReadingGoal": 350,
"totalMinutesRead": 5600,
"bookID": 65
},
{
"readerID": 2,
"name": "Daniel",
"weeklyReadingGoal": 210,
"totalMinutesRead": 3000,
"bookID": 65
}
]
书籍.json
[
{
"bookID": 65,
"title": "Goodnight Moon",
"author": "Margaret Wise Brown",
"publicationYear": "1953"
},
{
"bookID": 75,
"title": "Winnie-the-Pooh",
"author": "A. A. Milne",
"publicationYear": "1926"
}
]
目前,我可以从book.json中删除一本书
当我删除一本书,并且该书的 bookID 存在于reader.json对象中时,我想将该 ID 更新为 0。
例如,如果我删除 bookID = 65 的书,我想将 reader.json 中等于 65 的所有 bookID更改为 null。
以下是我的代码,如果需要,我可以提供其他代码:
书籍.js:
var data = getBookData();
var pos = data.map(function (e) {
return e.bookID;
}).indexOf(parseInt(req.params.id, 10));
if (pos > -1) {
data.splice(pos, 1);
} else {
res.sendStatus(404);
}
saveBookData(data);
var readerData = getReaderData();
var bookID = req.params.id;
console.log('BOOK ID - ' + bookID); // prints: BOOK ID - 47
readerData.forEach(function (x) {
if (x.bookID === bookID) {
x.bookID = null;
}
else {
console.log('Deleted book was not associated with any reader');
console.log('BOOK ID in else - ' + x.bookID); // prints: BOOK ID in else - 47
}
});
saveReaderData(readerData);
res.sendStatus(204);
});
function getReaderData() {
var data = fs.readFileSync(readerFile, 'utf8');
return JSON.parse(data);
}
当我运行此代码时,这本书已从books.json中删除,但 reader.json 中匹配的 bookID保持不变。
此外,在else
上面的块中,正在记录匹配的 bookID,但由于某种原因,代码没有流入if
块中。
解决方案
您可以循环阅读阅读器并检查bookID
. 这是你想要的吗?
您的错误是indexOf
在数组上使用,因为它只会向您返回第一个匹配项。
const readerData = [{
"readerID": 1,
"name": "Maria",
"weeklyReadingGoal": 350,
"totalMinutesRead": 5600,
"bookID": 65,
},
{
"readerID": 1,
"name": "Maria",
"weeklyReadingGoal": 350,
"totalMinutesRead": 5600,
"bookID": 70,
},
{
"readerID": 2,
"name": "Daniel",
"weeklyReadingGoal": 210,
"totalMinutesRead": 3000,
"bookID": 65,
},
];
const bookID = 65;
// Treat every reader
readerData.forEach((x) => {
// If the bookID is the one we are looking for, set it as null
if (x.bookID === bookID) {
x.bookID = null;
}
});
console.log(readerData);
所以。应用于您的代码
var readerFile = 'server/data/readers.json';
router.route('/')
.delete(function(req, res) {
var readerData = getReaderData();
var bookID = parseInt(req.params.id, 10);
// Have we found any occurence of the bookID ?
var found = false;
// Treat every reader
readerData.forEach(function(x) {
// If the bookID is the one we are looking for, set it as null
if (x.bookID === bookID) {
x.bookID = null;
found = true;
}
});
// If we haven't found any occurence, returns an error
if (!found) {
res.sendStatus(404);
return;
}
// Save the changes
saveReaderData(readerData);
res.sendStatus(204);
});
function getReaderData() {
var data = fs.readFileSync(readerFile, 'utf8');
return JSON.parse(data);
}
推荐阅读
- keras - keras ,不兼容的形状:[64] 与 [64,280]
- c# - 无法让这个简单的 ASP.NET 教程代码运行
- javascript - 在for循环中显示计数器的正确javascript是什么
- php - 将我的 Laravel 项目上传到服务器后出现错误
- c++ - 为什么复制列表初始化和复制初始化在这种情况下表现不同?
- node.js - Nodejs服务器不响应客户端get请求
- python - 熊猫多索引到 json
- android - React Native 中 textBreakStrategy 属性的简单、高质量、平衡有什么区别?
- android - Android JetPack 的共享 ViewModel 生命周期
- button - 在 Xamarin 表单中使用带有图像和文本的按钮