Promise with MySql2

Viewed 236

I'm a newbie at NodeJS and I wanted to know if I'm handling the promises and the alleged mistake well.

I write the piece of code...

Thank you very much for your feedback!

const pool = require('../config/db.config');

exports.get = async (req, res) => {

    var sql = 'SELECT * FROM Company WHERE CompanyCode = ?'
    const promisePool = pool.promise()
    try {
        const [rows,fields] = await promisePool.query(sql,req.params.companycode)
        if (rows.length === 0){
            res.status(404).json(rows)    
        } else {
            res.status(200).json(rows)    
        }
    } catch (err){
        res.status(500).json(err)    
    }
}
0 Answers
Related