简体   繁体   English

Select 来自数据库的数据取决于节点和 ejs 中的 select 值选项

[英]Select data from database depends on select value option in node and ejs

I am new in node.js. I use Express and EJS.我是node.js的新人,用的是Express和EJS。 I want to select value from select option and based this value select data from Postgres database and render on-page.我想从 select 选项中获取 select 值,并基于来自 Postgres 数据库的该值 select 数据并在页面上呈现。 How can I do that?我怎样才能做到这一点? Here is my code:这是我的代码:

EJS page EJS页面

<select name="company" class="form-control" id="companylist" onchange="getselectedvalue();">
          <% for (const comp of model1) { %>
            <option value='<%= comp.ic %>'><%= comp.name %></option>
          <% } %> 
</select>

server.js服务器.js

app.get("/users/dashboard",checkNotAuthenticated, (req, res) => {
const sql = `select * from company a join accountscompany b on a.id = b.id_company join accounts c on b.id_accounts = c.id WHERE c.id = $1`
pool.query(sql, [req.user.id], (err, com) => {
console.log(req.isAuthenticated());
res.render('dashboard', {user: req.user, content:"header", model1: com.rows});
});
});

So, for example: If I have to select value 1234 in select option value, then my query would be SELECT * FROM product WHERE company_ic = 1234 and render on my page.因此,例如:如果我必须在 select 选项值中将 select 值设为 1234,那么我的查询将是SELECT * FROM product WHERE company_ic = 1234并呈现在我的页面上。

Thanks for your help谢谢你的帮助

You should create the pool outside of the app.get.您应该在 app.get 之外创建池。 I would suggest in its own file where you can then call on it as needed.我会建议在它自己的文件中,然后您可以根据需要调用它。 I am assuming you are using the npm package mysql2我假设您使用的是 npm package mysql2
Also, I am assuming your sql syntax is correct.另外,我假设您的 sql 语法是正确的。 You did not give your table as reference.你没有给你的表作为参考。

require('dotenv').config();
const mysql = require('mysql2');
let pool = mysql.createPool({
    host: "IP_ADDRESS",
    user: "root",
    password: process.env.DBPASSWORD,
    database: "DBNAME",
    waitForConnections: true,
    connectionLimit: 20,
    queueLimit: 0
});
const con = pool.promise();

module.exports = con;

then something like this before app.get然后在 app.get 之前是这样的

const bodyParser = require('body-parser')
const con = require('./mysqlPool.js')
app.use(bodyParser.urlencoded({ extended: true }))

Then in your function, you can do:然后在您的 function 中,您可以执行以下操作:

app.get("/users/dashboard",checkNotAuthenticated, async (req, res) => {
    const com = await con.query('SELECT * FROM company a JOIN accountscompany b ON a.id = b.id_company JOIN accounts c ON b.id_accounts = c.id WHERE c.id = ?',[1234])
    .then((result)=>{
        return result[0];
    });
    res.render('dashboard', {user: req.user, content:"header", model1: com});
});

The 1234 is hard coded in the example above; 1234 在上面的例子中是硬编码的; however, you should obviously put in your req.query.[your variable name] variable in this.但是,您显然应该将 req.query.[your variable name] 变量放入其中。

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

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