Home > Mobile >  API keeps running even after success
API keeps running even after success

Time:03-24

I have an API the uses sequelize to edit a record:

router.put("/editTenant/:id", async(req,res) =>{
const id = req.params.id
const tenant = await Tenants.findByPk(id);
Tenants.update({
    tenantName: req.body.tenantName,
    industry: req.body.industry
  }, {
    where: {
     id: id
    }
   }
  );
})

When I use Insomnia to test it, the request keeps running but if I go to the database, I see that the update worked. Why does the request keep even after success?

CodePudding user response:

You need to end the request using res.send():

router.put("/editTenant/:id", async(req,res) =>{
  const id = req.params.id;
  const tenant = await Tenants.findByPk(id);
  Tenants.update({
    tenantName: req.body.tenantName,
    industry: req.body.industry
  }, {
    where: {
     id: id
    }
   }
  );

  res.send();
})

You should also catch all async errors and send them to the error middleware using next(err); take a look at the express error guide: https://expressjs.com/en/guide/error-handling.html

Here is also guide for express routing: https://expressjs.com/en/guide/routing.html

  • Related