简体   繁体   English

显示从节点js中的MySQL数据库检索的数据

[英]Displaying data retrieved from MySQL databased in node js

In the following code, I am trying to retrieve data from MySQL and display them in HTML page. 在以下代码中,我试图从MySQL检索数据并将其显示在HTML页面中。 I tested the connection by showing the data in the console and it worked fine but the problem is that the data cannot be displayed on the HTML page: 我通过在控制台中显示数据来测试连接,它可以正常工作,但是问题是数据无法显示在HTML页面上:

var http = require("http");
var mysql = require('mysql');

http.createServer(function (request, response) {

   response.writeHead(200, {'Content-Type': 'text/plain'});

   //response.end('Hello World\n');

   var connection = mysql.createConnection(
    {
      host     : 'localhost',
      user     : 'root',
      password : 'somepass',
      database : 'Students',
    }
);

connection.connect();

var queryString = 'SELECT * FROM Student';

connection.query(queryString, function(err, rows, fields) {
    if (err) throw err;

    for (var i in rows) {
        response.end('Name: ', rows[i].Name); //this doesn't work, it only shows Name: but doesn't show the retrieved name from the databased
        console.log('Name: ', rows[i].Name); //this works fine
    }
});

}).listen(8081);

console.log('Server running at http://127.0.0.1:8081/');

Can anybody help me solving this issue? 有人可以帮我解决这个问题吗?

Have you read the documentation? 您阅读过文档吗?

First of all, response.end takes three optional arguments: data, encoding and callback. 首先,response.end具有三个可选参数:数据,编码和回调。 What you are doing, is sending the data "Name: ", the encoding 'rows[i].Name'. 您正在做什么,正在发送数据“名称:”,编码为“ rows [i] .Name”。 That causes a problem in it self. 这本身就引起了问题。

In addition, you call this method in a for loop, which means the first time .end is called, you stop sending more. 另外,您在for循环中调用此方法,这意味着第一次调用.end时,您将停止发送更多方法。 As it says in the documentation, .end signals the end of data transfer, and that no more data is coming (But it is in your case). 就像在文档中所说的那样,.end表示数据传输已结束,并且不再有数据要发送(但这是您的情况)。

What you should do in stead, is to use response.write("Name: " + rows[i].Name); 相反,您应该使用response.write(“ Name:” + rows [i] .Name); You should make note of the following: 您应注意以下几点:

  1. I use .write, not .end, which means I can keep adding data to be sent. 我使用.write而不是.end,这意味着我可以继续添加要发送的数据。
  2. I use a +, and not a comma. 我使用+,而不是逗号。 That means I append the string rows[i].Name to the string "Name: ", so the final result will be "Name: ". 这意味着我将字符串rows [i] .Name附加到字符串“ Name:”,因此最终结果将是“ Name:”。

Finally, when the for loop is done, you should call response.end() with no arguments, indicating that you are finished with the message to be sent. 最后,当for循环完成时,应不带任何参数调用response.end(),这表明您已完成要发送的消息。

Hope this helps, and take a closer look at the documentation next time as it will probably explain these things quite clearly :) Happy programming! 希望这会有所帮助,下次再仔细阅读该文档,因为它可能会很清楚地解释这些内容:)编程愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM