Nodejs + Mysql + Return of Database Queries

Asked

Viewed 702 times

1

Good afternoon! I’m having some problems with NODEJS using a Mysql database, I’ve made several attempts and I’m not sure how to solve this problem. My problem is that I would like to consume the data return from a SELECT table in the back end but I’m not getting it. I can print the information on the screen using Insomnia (Postman) but I can’t bring the information to the backend and use it.

Query I am using to query in the database (note that in this layer I already do console.log but later will return 'Undefined')

Database settings (hidden but working correctly) and the function to execute the query, on this screen I ask to print in JSON information and it returns normal.

Feedback on the Front-End screen, ok

Console information.log I asked to print in the back end earlier, already returns me UNDEFINED

When I ask to print the variable 'res', just show me all this information

Well, I hope you can help me, thank you very much!

  • I don’t understand what you want me to do? What I expect?

  • I wish I could at the backend layer, be able to handle the result of the query, but I’m not able to bring it because it returns Undefined

  • you are wanting to bring the result of database return so that you can view in the console this data?

  • Yeah, that’s right, that’s right

1 answer

1


One model I use is the following

  • connection.js:

const mysql = require("mysql");

const connection = mysql.createConnection({
  host: "localhost",
  port: "3306",
  database: "db",
  password: "senha",
  user: "root",
});

const query = (sql, callBack) => {
  return connection.query(sql, callBack);
};

module.exports = {
  connection,
  query,
};
  • TodoController.js

const { query } = require("../connection");

module.exports = {
  get: function (req, res) {
    query("SELECT * FROM todos", function (error, result, field) {
      console.log(result); // resultado obtido
      if (error) {
        res.json(error);
      } else {
        res.json(result);
      }
    });
  },
};
  • express:

var express = require("express");
var app = express();

const TodoController = require("./controllers/TodoController");

app.get("/", function (req, res) {
  res.send("Service Init ...");
});

app.get("/todo", TodoController.get);

app.listen(3000, function () {
  console.log("Service Web Port 3000!");
});

view the information also on the console, by the line placed inside the TodoController.js.


From what I could tell if you did the wrong part:

const teste = mysql.execSQLQuery("SELECT * FROM kit", res);
                                                       ^
                                                       |

passing the res and this may be going wrong because there is no variable response is a function that has the following parameters

function(error, result, field) { } 

and in that moment the problem, you have to follow the same function that you yourself stated in the images.

  • 1

    Thank you very much, this was the problem! I changed the way you gave me and it worked super well, thank you very much!.

Browser other questions tagged

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