Node.js How to handle multiple query results

Asked 2 years ago, Updated 2 years ago, 48 views

Node.js, using express.

Put the db result into the variable value in conjunction with mysql I'd like to send it to render.

But to try the global variable.I even marked it with a score As for the result, there is no query result as below, so I'm asking you a question.

I'm asking this question because I've only been exposed to node.js for about 3 days, so I'm not familiar with how to use callback. Please let me know how I can call up all the results of the query.

[Result]

]}', {"scoredboard": [}

[Source Code]

var express = require('express');
var router = express.Router();
//db - mysql
var mysql = require('mysql');
var dbconfig = require('../dbconfig.js');
var db = dbconfig.database;
var connection=mysql.createConnection({
   host:db.host,
   port:db.port,
   user:db.user,
   password:db.password,
   database:db.database
});


/* /* GET home page. */
router.get('/', function(req, res, next) {
  /*
{"scoreboard": [{"tid": 68, "position": 1, "score": 200, "name": "A", "history": [{"score": 200, "when": "2016-04-01T17:17:42+00:00"}, {"score": 150, "when": "2016-03-28T17:19:04+00:00"}, {"score": 50, "when": "2016-03-27T17:24:33+00:00"}, {"score": 10, "when": "2016-03-23T17:32:48+00:00"}, {"score": 5, "when": "2016-03-22T17:24:33+00:00"}]}, {"tid": ": 3, "position": 2, "score": 0, "name": "B", "history": [{"score": 0, "when": "2016-03-23T17:18:26+00:00"}]}
*/
  global.scoreboard = '{\"scoredboard\" : [';
  var position = connection.query('select tid, score, name from teams order by (score+0);', function(err, rows_tid)
  {
      if(err)
      {
          console.error(err);
          throws(err);
      }
      else
      {
          for(var i in rows_tid)
          {
              global.scoreboard = global.scoreboard + '{\"tid\": ' + rows_tid[i].tid + ', \"position\": ' + i + ', \"score\": ' + rows_tid[i].score + ', \"name\": \"' + rows_tid[i].name + '\", \"history\": [';
              var query = connection.query('select score,DATE_FORMAT(wh, \'%Y-%m-%dT%T\') as wh from scoreboard where tid = ? order by wh',rows_tid[i].tid, function(err, rows)
              {
                  if(err)
                  {
                      console.error(err);
                      throws(err);
                  }
                  else
                  {
                      for(var j in rows)
                      {
                          global.scoreboard = global.scoreboard + '{\"score\": ' + rows[j].score + ', \"when\": ' + rows[j].wh + '}';
                          if(j != rows.length-1)
                          {
                              global.scoreboard = global.scoreboard + ', ';
                          };
                      };
                  };
              });
              global.scoreboard = global.scoreboard + ']}';
              if (i != rows_tid.length-1)
              {
                  global.scoreboard = global.scoreboard + ', ';
              }
          };
      };
  });
  global.scoreboard = global.scoreboard + '}';
  res.render('scoreboard', {scoreboard:global.scoreboard});
});

module.exports = router;

node.js

2022-09-21 16:39

1 Answers

The reason for the blank result in the above code is that res.render function is called at the time global.This is because the scoreboard variable is not ready yet.

Requests entered as router.get terminate at the same time the res.render function is invoked.

The problem with the above code is that it invokes the res.render function without waiting for the result of connection.query.

Because connection.query is an asynchronous function, the code stops on that line and continues to the next line without waiting for results. Subsequent execution results are communicated through the callback function.

Therefore, to resolve the problem, connection.You must invoke the res.render function using the query result (rows_tid) passed inside the callback function (function(err, rows_tid) passed to the query function.

If you understand all of the above stories, all the problems are solved.

In the above code, there is an additional slightly more complicated part: connection.The point where query was used overlappingly. You can solve these parts by hand, but you can usually solve them by using the async module or promise.

The most obvious asynchronous solution in Node.js is to use coroutine, which is recommended if you are familiar with the above modules.

p.s: Global variables are not required to troubleshoot asynchronous problems.


2022-09-21 16:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.