简体   繁体   English

如何在浏览器中的 Node.js 中显示 MySQL 数据库中的数据

[英]How to display Data from MySQL database in Node.js in browser

I want to display DB data in my browser .. my file connection.js is我想在我的浏览器中显示数据库数据..我的文件 connection.js 是

var mysql = require('mysql');

var conn = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "0000",
    database: "select_country"
});
conn.connect(function (err) {
    if (err) throw err;
    console.log('Database is connected successfully ??????!');
});
module.exports = conn;

and my app.js is :我的 app.js 是:

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const mysql = require('./MySql/database');

//create a server object:
const server = http.createServer(function (req, res) {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    mysql.query("SELECT * FROM country", function (err, result, fields) {
        if (err) throw err;
        console.log('All Countries are :-');
        res.write(result); //write a response to the client
    });

    res.write('Hello World'); //write a response to the client
    res.end(); //end the response
}); //the server object listens on port 8080

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

Error : throw er;错误:抛出错误; // Unhandled 'error' event,,,, Error [ERR_STREAM_WRITE_AFTER_END]: write after end // 未处理的 'error' 事件,,,, Error [ERR_STREAM_WRITE_AFTER_END]: write after end

You must only write your responses to the client from within the callback function of mysql.query() .您只能在mysql.query()的回调函数mysql.query()您的响应写入客户端。

In other words, your program runs these two lines immediately换句话说,你的程序会立即运行这两行

res.write('Hello World'); //write a response to the client
res.end(); //end the response

before MySQL gets its data the callback runs.在 MySQL 获取其数据之前,回调运行。 Your res.end() ends the output.您的res.end()结束输出。 Therefore, later, when the callback runs, it tries to res.write() to an already ended stream.因此,稍后,当回调运行时,它会尝试将res.write()写入已经结束的流。

So move those two lines to within your callback.因此,将这两行移到您的回调中。 Try this尝试这个

    mysql.query("SELECT * FROM country", function (err, result, fields) {
        if (err) throw err;
        res.write('Hello World'); //write a response to the client
        console.log('All Countries are :-');
        res.write(result); //write a response to the client
        res.end(); //end the response
    });

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

相关问题 我们如何使用node.js从angular 7应用程序中的mysql数据库中获取数据并将其显示在angular组件html页面上 - How can we get data from mysql database in angular 7 application by using node.js and display it on angular component html page Node.js - 从数据库中检索数据(mysql) - 延迟 - Node.js - Retrieving data from a database (mysql) - delay 如何在撇号中转义以匹配Node.js / Discord.js中MySQL数据库的数据? - How can I escape an apostrophe to match data from a MySQL database in Node.js/Discord.js? 使用Node.js显示MySQL数据 - Display MySQL data using Node.js 如何使用ReactJS,Node.js和MySQL查询将数据从表单数据发送到数据库? - How to send data from a form data to a database using ReactJS , Node.js and MySQL queries? 如何从 MySQL 中提取数据并将其显示在带有 Node.js 的网页上? - How do I extract data from MySQL and display it on a webpage with Node.js? Node.js + MySQL数据未进入数据库 - Node.js + mysql data is not entering into database 使用node.js将数据添加到mysql数据库 - Adding data to mysql database with node.js 如何从 Node.js 向 MySQL 数据库中插入汉字? - How to insert Chinese characters into a MySQL database from Node.js? 如何在Node.js应用程序中将数据从Redis保存到MySQL - How to persist data from Redis to MySQL in a Node.js app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM