Query mysql INSERT returns nothing, even after

Asked

Viewed 38 times

0

I recently started using Ode and I’m kind of having a little problem.

I’m trying to insert a product into my "products" table but I didn’t put auto-increment in its ID, so to save the right ID I’m using first a "SELECT MAX", something like:

router.post('/', (req, res) => {
    const { produto_descricao, produto_preco_venda, produto_marca}
    conn.query('SELECT MAX(id_produto) as id_produto FROM produtos', (error, results) => {
       if (error) res.json(error);

       let id_max = results[0].id_product
       id_max = id_max === null ? 1 : id_max + 1;

       conn.query(
          `INSERT INTO produtos (id_produto, produto_descricao, produto_preco_venda, produto_marca) VALUES ('${id_max}', '${produto_descricao}', '${produto_preco_venda}', '${produto_marca}')`,
          (error, results) => {
           if (error) res.json(error);

          res.json({ message: "Everything ok!" })
      })`
   })
})

And then I’m running another query inside the previous one to actually give the INSERT in the database.

But this code writes to the database normally, but it’s as if the res.json({ message: "Everything ok!" }) if ignored, it returns nothing.

What am I doing wrong?

  • Tries putting a Return in the res.json({ message: "Everything ok!" }) and if possible place the if with { } passing the error as it did and at the bottom line inserts a Return inside if tbm.

  • Unfortunately not solved. Still it records in the bank but no response. :/

  • SELECT MAX(id_produto) as id_produto but when you read results[0].id_product, already here ta wrong is id_product or id_product? Besides this, what are those constants declared at the beginning that hold no value?

  • It is id_product, I wrote wrong when asking the question. constants take the value of the body and are being sent in INSERT.....

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.