【发布时间】:2020-12-01 23:28:01
【问题描述】:
如何使用 Postman 更新我的数据库?
我正在尝试使用 Postman 学习 Express 来测试不同类型的请求,但我不知道如何更新我的数据集。这是我正在使用的数据集的结构。
var DB = [
{ category: 'Pets',
products: [
{ name: 'banjo',
color: 'grey',
mean: true,
description: "meows"
},
{ name: 'rigby',
color: 'black and white',
mean: false,
description: 'barks'
}]}]
Lets say I want to add another pet into the pets category so in products
{name: frank, color: orange, : mean: false: description: glubs}
我不知道如何在 Postman 中正确添加它以便更新。我的更新代码如下:
app.post("/product/add", (req, res) => {
var category = req.body.category;
const { name, color, mean, description } = req.body;
var product = { name:name, color:color, mean:mean, description:description }; console.log(product);
var index = DB.findIndex(x => x.category == category);
if(index !== -1){
var indexProduct = DB[index].products.findIndex(x => x.name == product.name); if(indexProduct !== -1){
DB[index].products.push(product);
res.status(200).send(DB);
} else {
res.status(200).send(`Product already added to category.`);
};
} else {
res.status(200).send(`Category not found.`);
} });
提前致谢!格式也很抱歉。
【问题讨论】:
-
请正确格式化发布的代码,现在真的很难阅读。