Home > database >  Method from my API in express mariadb don't work
Method from my API in express mariadb don't work

Time:04-08

I have an API in MariaDB and ExpressJS and just works get route...

const getAllBills = async (req, res) => {
    try {
      const conn = await pool.getConnection();
      const result = await conn.query('SELECT * FROM facturas')
      res.status(200).json(result);
    } catch (err) {
      res.status(500).json({
        message: err.message
      });
    }
  };

const createBills = async (req, res) => {
    try {
      const conn = await pool.getConnection();
      const result = await conn.query(
      'INSERT INTO facturas (total, nombre, apellido, ciudad, direccion, codigo_postal, cedula) VALUES (?, ?, ?, ?, ?, ?, ?)', 
      [req.body.total, req.body.nombre, req.body.apellido, req.body.ciudad, req.body.direccion, req.body.codigo_postal, req.body.cedula]
    );
    
    const deleteBills = async (req, res) => {
    try {
      const conn = await pool.getConnection();
      const result = await conn.query('DELETE FROM facturas WHERE id = ?', [req.params.id]);
      if (result.affectedRows === 0) {
        res.status(404).json({
          message: 'Factura no encontrada'
        });
      } else {
        res.status(200).json(result);
      }
    } catch (err) {
      res.status(500).json({
        message: err.message,
      });
    }
  }

These are my routes, it works except post and delete

const { Router } = require('express');
const router = Router();

const { getAllBills, getBillsByCedula, createBills, deleteBills } = require('../controllers/facturaControllers');

router.get('/facturas', getAllBills);

router.post('/facturas', createBills);

router.delete('/facturas/:id' , deleteBills);

CONSOLE POST

CONSOLE DELETE

Then, I don't know what to do because the server really works but when I make a POST or DELETE request I get error 500.

CodePudding user response:

This code lacks a } sign, and the function/logic of creatbills and delete bills in controllers must be separated.

Try using this code:

const createBills = async (req, res) => {
    try {
      const conn = await pool.getConnection();
      const result = await conn.query(
      'INSERT INTO facturas (total, nombre, apellido, ciudad, direccion, codigo_postal, cedula) VALUES (?, ?, ?, ?, ?, ?, ?)', 
      [req.body.total, req.body.nombre, req.body.apellido, req.body.ciudad, req.body.direccion, req.body.codigo_postal, req.body.cedula]
    );
    } catch (err){
        res.status(500).json({
            message: err.message,
          });
    };
};
    
const deleteBills = async (req, res) => {
    try {
      const conn = await pool.getConnection();
      const result = await conn.query('DELETE FROM facturas WHERE id = ?', [req.params.id]);
      if (result.affectedRows === 0) {
        res.status(404).json({
          message: 'Factura no encontrada'
        });
      } else {
        res.status(200).json(result);
      }
    } catch (err) {
      res.status(500).json({
        message: err.message,
      });
    };
};

  • Related