Gustavo Bordin
Gustavo Bordin

Reputation: 109

Sequelize - try catch not handling error if foreignKey value is invalid

I have a POST route to create documents, in this route, a have a condition, if i receive an array of tags, i insert these tags with the method that Sequelize gives me when i create a many to many relationship, in my case, Sequelize gave me addTags.

I am sending the JSON data this way:

{
    "description": "10000000000000",
    "content": "conteudo1",
    "groupId": 2,
    "tags": [1,2,3,4]
}

The tags with id 1,2,3 exists, but not with id 4, so it should throws an error that would be catched by try catch and passed to my middleware which parses the error and returns its message.

My code:

async create(req, res, next){
     const { tags, ...data } = req.body

     try {
         const document = await Documents.create(data)
         if(tags) {
              document.addTags(tags) 
         }
         return res.status(201).send()

     } catch(error) { next(error) }
       
 }

The problem is that the error generated in document.addTags(tags) is not being catched, so even with error, it returns me status 201, and not 500, which is inside my middleware being called in catch

The error:

(node:1856) UnhandledPromiseRejectionWarning: SequelizeForeignKeyConstraintError: insert or update on table "documentTags" violates foreign key constraint "documentTags_tagId_fkey"
    at Query.formatError (C:\Users\arma_\OneDrive\Documentos\githubProjects\api-nodejs\node_modules\sequelize\lib\dialects\postgres\query.js:315:16)
    at Query.run (C:\Users\arma_\OneDrive\Documentos\githubProjects\api-nodejs\node_modules\sequelize\lib\dialects\postgres\query.js:87:18)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:1856) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

Upvotes: 1

Views: 1147

Answers (1)

Karthik Radhakrishnan
Karthik Radhakrishnan

Reputation: 944

Since document.addTags(tags) is containing database operations. These operations will be run in synchronous manner. That is by default, it will continue executing the following lines without waiting for its completion. and res.send() will get executed even before the document.addTags gets completed. you can either use await to wait for the execution to get complete or you can use callbacks.

async create(req, res, next){
     const { tags, ...data } = req.body

     try {
         const document = await Documents.create(data)
         if(tags) {
              await document.addTags(tags) 
         }
         return res.status(201).send()

     } catch(error) { next(error) }
       
 }

Upvotes: 1

Related Questions