将查询结果从MySQL发送到.ejs

时间:2022-10-13 12:55:09

I've just begun learning node.js and was trying to fetch query results from MySQL and send them to an ejs file. My original app.js looked like this.

我刚刚开始学习node.js,并试图从MySQL获取查询结果并将它们发送到ejs文件。我原来的app.js看起来像这样。

Here's my original app.js:

这是我原来的app.js:

var app = express();
var client = mysql.createConnection({
    user: 'user',
    password: '',
    database: 'DB_practice'
});
app.get('/', function(request, response){
    var result;
    fs.readFile('list.ejs', 'utf8', function(error, data){

        client.query('SELECT * FROM test', function(error, results){
            result = results;
            console.log("Query results(inside): " + JSON.stringify(results));             
        });
        console.log("Query results(outside): " + JSON.stringify(results));
        response.render(__dirname + '/list', {data: result} );

    });
});

But I noticed that having the render() method outside the client.query() block didn't execute the code in order. console.log("Query results(inside): " + JSON.stringify(results)); line prints the results out fine, in an array form. But when I print it outside(console.log("Query results(outside): " + JSON.stringify(results));), it returns undefined, which would mean that the "inside" print function is executed after "outside" print function. So I tried placing the render() inside the query block as such:

但是我注意到在client.query()块之外使用render()方法没有按顺序执行代码。 console.log(“查询结果(内部):”+ JSON.stringify(results)); line以数组形式打印结果。但是当我在外面打印它时(console.log(“查询结果(外部):”+ JSON.stringify(results));),它返回undefined,这意味着“内部”打印函数在“外部”之后执行打印功能。所以我尝试将render()放在查询块中:

app.get('/', function(request, response){
    var result;
    fs.readFile('list.ejs', 'utf8', function(error, data){

        client.query('SELECT * FROM test', function(error, results){
            result = results;
            response.render(__dirname + '/list', {data: results} );
            console.log("Query results(inside): " + JSON.stringify(results));             
        });
        console.log("Query results(outside): " + JSON.stringify(results));


    });
});

which complains the variable to which I pass the query results in list.ejs is undefined. Here's the portion from list.ejs:

抱怨我在list.ejs中传递查询结果的变量是未定义的。这是list.ejs中的部分:

    <script type='text/javascript'>
        var row =<%-JSON.stringify(data)%>
    </script>
    <% row.forEach(function(item, index){ %>
    <tr>
        <td><a href="/delete/<%= item.id %>">DELETE</a></td>
        <td><a href="/edit/<%= item.id %>">EDIT</a></td>
        <td><%= item.id %></td>
        <td><%= item.name %></td>
        <td><%= item.phone_no %></td>
    </tr>
    <% }); %>

It says that row is undefined.

它说行是未定义的。

I've been struggling with this for a couple days. Any help would be greatly appreciated.

几天来我一直在努力解决这个问题。任何帮助将不胜感激。

1 个解决方案

#1


0  

Just update your list.ejs with given below code -

只需使用以下代码更新您的list.ejs -

list.ejs

<% data.forEach(function(item, index){ %>
<tr>
    <td><a href="/delete/<%= item.id %>">DELETE</a></td>
    <td><a href="/edit/<%= item.id %>">EDIT</a></td>
    <td><%= item.id %></td>
    <td><%= item.name %></td>
    <td><%= item.phone_no %></td>
</tr>
<% }); %>

Anything within tags <%- %> will print to output with unescaped HTML data.

标签<% - %>内的任何内容都将使用未转义的HTML数据打印输出。

#1


0  

Just update your list.ejs with given below code -

只需使用以下代码更新您的list.ejs -

list.ejs

<% data.forEach(function(item, index){ %>
<tr>
    <td><a href="/delete/<%= item.id %>">DELETE</a></td>
    <td><a href="/edit/<%= item.id %>">EDIT</a></td>
    <td><%= item.id %></td>
    <td><%= item.name %></td>
    <td><%= item.phone_no %></td>
</tr>
<% }); %>

Anything within tags <%- %> will print to output with unescaped HTML data.

标签<% - %>内的任何内容都将使用未转义的HTML数据打印输出。