首页 > 解决方案 > 尝试使用邮递员更新我的 Express 数据库

问题描述

如何使用 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.`);     
} }); 

提前致谢!也对不起格式。

标签: expresspostman

解决方案


代码中有很多重复、不需要的解构变量和语法错误。首先将您的问题分解成更易于管理的小块,然后使用邮递员测试您的端点。

让我们从您的数据及其结构开始:

var DB = [{
category: 'Pets',
products: [{
        name: 'banjo',
        color: 'grey',
        mean: true,
        description: "meows"
    },
    {
        name: 'rigby',
        color: 'black and white',
        mean: false,
        description: 'barks'
    }
  ]
}]

// 假设您想将另一个宠物添加到宠物类别中,因此在产品中

const obj = {
    name: "frank",
    color: "orange",
    mean: false,
    description: "glubs"
}

这是您可以检查是否找不到对象的方法之一,如果没有,则将其添加到您的数据库中。

DB.forEach((val) => {
   if (val.products.name !== obj.name) {
       DB[0].products.push(obj);
   } 
   
})

console.log(DB[0].products)

/**
 *[{
         name: 'banjo',
         color: 'grey',
         mean: true,
         description: 'meows'
     },
     {
         name: 'rigby',
         color: 'black and white',
         mean: false,
         description: 'barks'
     },
     {
         name: 'frank',
         color: 'orange',
         mean: false,
         description: 'glubs'
     }
 ]

*/

您的邮递员请求可能如下所示:

app.post("/product/add", (req, res) => {  
  // extract just what you need e.g name or ID if you have one...  
  const { name } = req.body;   
  // NOT efficient, best if you use a unique ID to look up  
  DB.forEach(value => {
     if (value.products.name !== name) {
        DB[0].products.push(req.body);
        return res.status(200).send(JSON.stringify(DB));
     }
     else
        return res.status(404).send("Not found or whatever");
  });
 });     
         

或者使用Array.prototype.findIndex你可以做:

app.post("/product/add", (req, res) => {   
  const { name } = req.body;   
  const index = DB.forIndex(value => value === name);
  if (index === -1) {
    DB[0].products.push(req.body);
    return res.status(200).send(JSON.stringify(DB));
  }
    
  else
    return res.status(404).send("Not found or whatever");
  }
 });     

注意:如果您的对象名称与另一个对象名称相同,您的新对象将不会被推送到数据库数组。这建议您需要使用唯一标识符来索引您的对象。

如果此答案可以帮助您解决问题,考虑接受该答案。


推荐阅读