B
B
BosonBeard2014-06-30 00:48:57
MySQL
BosonBeard, 2014-06-30 00:48:57

How to pull data from a database query using the connection.query function in node.js (except for conslole.log)?

Colleagues, I am not a programmer, but it is very necessary for production needs.
I understand that there is a similar question, but I can’t understand the answers in it, and in general,
the understanding of callback functions has not been given to me for the 4th day.
Tell me I use node-webkit with the mysql module installed
in the example I took something like this:
index.html

!DOCTYPE html>
<html>
  <head>
    <title>Hello World2!</title>
     <script>
        
    
    function bodyOnLoad(){
           
    
        misc=require('./mysql.js');
        	
        }
  
    </script>
  </head>
   <body onload="bodyOnLoad();"  >
  </body>
</html>

mysql.js
var mysql = require('mysql');
var connection = mysql.createConnection({
 database : 'techtrain',
   user     : 'root',
   password : ''
});


connection.connect();

connection.query('SELECT * FROM `answer` WHERE `question_id` = 2', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].answer_name);
});

connection.end();

so, when executed in node-webkit, it gives the desired result:
"The solution is: 1 time in 1 year"
The problem is that I can’t pull this data into the index.html file
in any way, and in general I can’t pull it out of the
connection function .query('SELECT * FROM `answer` WHERE `question_id` = 2', function(err, rows, fields)
i.e. inside the function everything is super all data is available,
but when you try to output it to a global variable,
or somehow also, to see them beyond this function. but it is desirable so that I can’t access them from the main index.html file.
Already, I just didn’t dig, but I don’t understand the mechanism, but I really need to process them somehow.
If anyone There is something wise and understanding, please help.
In those examples of working output to the console, I just do not understand how to output to an accessible variable.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
W
w3d12b, 2014-06-30
@w3d12b

You need to understand the mechanism for exporting the result of a function from within to the external environment.
1. Documentation of the nodejs API "modules"
2. Explanations on Habré
And in the html file, the DOM API innerHTML

var mysql = require('mysql');

var connection = mysql.createConnection({
  database : 'mysql',
   user     : 'root',
   password : 'toor'
});

var x={};

var query = function(){
  connection.connect();
  connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
    if (err) throw err;
    x = rows[0].solution;
  });
  connection.end();
};

var answer = function(){
  return x;
};

module.exports.query = query;
module.exports.answer = answer;

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World2!</title>
 <script>
    var mysql = require('./mysql.js');
    function bodyOnLoad(){
      mysql.query();
    };

    function getAnswer(){
      document.getElementById("answer").innerHTML = mysql.answer();
    }
    </script>
  </head>
   <body onload="bodyOnLoad();">
   <div id="answer" onClick="getAnswer;">Click me</div>
  </body>
</html>

But this is a disposable crutch. To work with asynchronous callback functions,
Evansive 's approach is more elegant and easier to develop further. And it's easier if you sit down and understand the concept, and your perseverance and determination is quite impressive.
There are materials on Habré if it is difficult with English.

E
Evansive, 2014-06-30
@Evansive

app.get('/page', function(req, res) {
    connection.query("SELECT * FROM `table`",  function(err, result) {
        res.render('index.html', { object: result });
    });
});

B
BosonBeard, 2014-06-30
@BosonBeard

Colleagues, but at least someone can give an explicit answer, how can I extract and process the
"rows[0].answer_name" key in this example with anything other than console.log? I've been wasting my time for 5 days now

G
green_turtle, 2014-06-30
@green_turtle

Read more about closures and callback functions (closure, callback)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question