0

I'm trying to return a value from mysql using node and a query but i got an error that says nothing to me, i believe that is something related to my query but, i don't know what, can someone please help me? I've dig in a lot of places but didn't find anything that could help me. This is the code:

let queryIdBairro = "SELECT id FROM bairros ORDER BY id DESC LIMIT 1;"
                    conn.execSQLQuery(queryIdBairro, function(result, err) {
                        if(result){
                            bairro = result[0].id

                            let cadastraFrete = "INSERT INTO fretes_bairros (idempresa, idbairro, frete, status) VALUES (" + empresa + ", " + bairro + ", " + valor + ", " + 1 + ");"
                            conn.execSQLQuery(cadastraFrete, function(err, result) {
                                if(result){
                                    console.log("[" + new Date() + "] " + "BAIRRO COM O ID "  + bairro.id + "CADASTRADO COM SUCESSO")
                                    resolve(bairro)
                                } else if(err){
                                    console.log("DEU RUIM PARA SALVAR O FRETE AMIGUINHO... [" + new Date() + "] " + err)
                                }
                            })
                        } else if(err){
                            console.log("DEU RUIM NA PRIMEIRA BUSCA DO NOVO BAIRRO AMIGUINHO... [" + new Date() + "] " + err)
                        }
                    })

In the console i got this:

[Mon Jun 29 2020 13:50:31 GMT-0300 (GMT-03:00)] [object Object]

I'm pretty new on node and have no idea about this error or how to fix it exactly. Any help will be very appreciated.

Thanks in advance.

Gabriel Sassaki
  • 123
  • 1
  • 11
  • First you need to see what is the error but you can't see it because you are trying to concatenate an object with an string. Please do a console.log(err) in an isolated line to see the error. – Ivan Lynch Jun 29 '20 at 17:19
  • It looks like barrio.id is an object. What do you get when you put in console.dir(barrio.id)? Also, why are you executing a SQL query and then not using the result? – drnugent Jun 29 '20 at 17:19
  • If you just want to console the data: `JSON.stringify(bairro.id)` or for err `JSON.stringify(err)` in console.log – wrangler Jun 29 '20 at 17:34
  • @IvanLynch, thanks for your answer, if log err i got [object Object]... – Gabriel Sassaki Jun 29 '20 at 17:37
  • @drnugent, thanks for your answer also, you're right, bairro.id was an object, i took the .id off, I'm using the result in the insert query, I'm using it wrong? Anyways, i find an answer, don't if is right but it works just don't understand why, I'll post the answer if someone can help understand I'll be very happy... – Gabriel Sassaki Jun 29 '20 at 17:37
  • conn.execSQLQuery(cadastraFrete, function(result, err) { if(result){ console.log(JSON.stringify(result)) } else if(err){ console.log(JSON.stringify(err)) } }) – Ivan Lynch Jun 29 '20 at 18:17
  • Can you tell us what it return – Ivan Lynch Jun 29 '20 at 18:18

1 Answers1

0

Thanks everyone for your answers, i find a workaround, don't know if it's right but works and I don't know why, can someone help understand? Here's the code:

let queryIdBairro = "SELECT id FROM bairros ORDER BY id DESC LIMIT 1;"
                    conn.execSQLQuery(queryIdBairro, function(result, err) {
                        //console.log(result, err)
                        if(err){
                            bairro = err[0].id
                            let cadastraFrete = "INSERT INTO fretes_bairros (idempresa, idbairro, frete, status) VALUES (" + empresa + ", " + bairro + ", " + valor + ", " + 1 + ");"
                            conn.execSQLQuery(cadastraFrete, function(err, result) {
                                if(result){
                                    console.log("[" + new Date() + "] " + "BAIRRO COM O ID "  + bairro + " CADASTRADO COM SUCESSO")
                                    resolve(bairro)
                                } else if(err){
                                    console.log("DEU RUIM PARA SALVAR O FRETE AMIGUINHO... [" + new Date() + "] " + err)
                                }
                            })
                        } else if(result){
                            console.log("DEU RUIM NA PRIMEIRA BUSCA DO NOVO BAIRRO AMIGUINHO... [" + new Date() + "] " + err)
                        }

As you can see, I just change result and err on the if statement

Why?

Gabriel Sassaki
  • 123
  • 1
  • 11
  • 1
    Your code is **vulnerable** to **sql injection** you should always use **prepared staemente with parameters** see https://stackoverflow.com/questions/15778572/preventing-sql-injection-in-node-js – nbk Jun 29 '20 at 17:55
  • @nbk, thanks for your comment, honestly, i still don't know how to perform this, but I will learn and start using it... – Gabriel Sassaki Jun 29 '20 at 18:17