Node.js MySQL Error Handling

后端 未结 6 1793
遇见更好的自我
遇见更好的自我 2020-12-08 22:30

I\'ve read several examples for using mysql in node.js and I have questions about the error handling.

Most examples do error handling like this (perhaps for brevity)

6条回答
  •  醉酒成梦
    2020-12-08 23:07

    I guess this method is more approachable. In this case even if you fail to gain connection you throw an Internal Server Error status to client(helpful if you building a Rest Api Server) and in case there is a query error after releasing connection your sending the error. Please correct me if am wrong anywhere.

     pool.getConnection(function(err, connection){
          if(err){
            console.log(err);
            return res.status(500).json();
          };
    
    
          connection.query('SELECT * FROM TABLE', function(err,results,fields){
            connection.release();
    
            if(err){
              console.log(err);
              return (res.status(500).json());
            };
            res.status(201).send('OK');
    
          });
    
    
       });
    

提交回复
热议问题