简体   繁体   English

Node.js和Ejs,如何处理2个表?

[英]Node.js & ejs, how to handle 2 tables?

Here is a question about node.js that I need to answer. 这是我需要回答的有关node.js问题。 Since I am still a beginner in the field, it may actually be quite easy. 由于我仍然是该领域的初学者,因此实际上可能很容易。

Following some documentation that I found online, I have this code (inside a file called index.js): 在网上找到一些文档之后,我有了这段代码(在一个名为index.js的文件中):

app.get('/db', function (request, response) {
  pg.connect(process.env.DATABASE_URL, function(err, client, done) {
    client.query('SELECT * FROM test_table', function(err, result) {
      done();
      if (err)
       { console.error(err); response.send("Error " + err); }
      else { 
        response.render('pages/db', {results: result.rows} ); 
      }
    });
  });
});

and I also have this other file called db.ejs: 而且我还有另一个名为db.ejs的文件:

<!DOCTYPE html>
<html>
<head>
</head>

<body>

<div class="container">
<h2>Database Results</h2>

<ul>
    <% results.forEach(function(r) { %>
        <li><%= r.id %> - <%= r.name %></li>
    <% }); %>
</ul>

</div>

</body>
</html>

By some magic between the two I can display the contents of test_table. 通过两者之间的某种魔术,我可以显示test_table的内容。 It works as one would expect. 正如人们所期望的那样。

Now here comes my question: 现在是我的问题:

Along with the query: 'SELECT * FROM test_table' I also want to add a second query: 'SELECT * FROM other_test_table' and I want to be able to display the contents of other_test_table after (or before) that of test_table. 除了查询: “ SELECT * FROM test_table”之外,我还想添加第二个查询: “ SELECT * FROM other_test_table”,并且我希望能够在test_table之后(或之前)显示other_test_table的内容。 How do I need to change the code in both places for that? 我该如何在两个地方都更改代码?

You can try: 你可以试试:

app.get('/db', function (request, response) {
    pg.connect(process.env.DATABASE_URL, function(err, client, done) {
        client.query('SELECT * FROM test_table', function(err, result1) {
            if (err) {
                done();
                console.error(err);
                response.send("Error: " + err.message);
            } else {
                client.query('SELECT * FROM other_test_table', function(err, result2) {
                    done();
                    if (err) {
                        console.error(err);
                        response.send("Error: " + err.message);
                    } else { 
                        response.render('pages/db', {
                            results1: result1.rows,
                            results2: result2.rows
                        }); 
                    }
                });
            }
        });
    });
});

and in db.ejs : 并在db.ejs

<!DOCTYPE html>
<html>
<head>
</head>

<body>

<div class="container">
<h2>Database Results</h2>

<ul>
    <% results1.forEach(function(r) { %>
        <li><%= r.id %> - <%= r.name %></li>
    <% }); %>
</ul>

<ul>
    <% results2.forEach(function(r) { %>
        <li><%= r.id %> - <%= r.name %></li>
    <% }); %>
</ul>

</div>

</body>
</html>

Hope it helps. 希望能帮助到你。

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

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