ExpressJS:我想从数据库获取数据
我正在使用 express,eJs和sqlite 进行一个项目,因此我制作了从数据库中获取数据的搜索操作函数,我想在按下按钮而不是按下按钮时显示数据是我制作的功能:
function findHomeforSell(req , res , next){
const sell = "sell";
db.all("SELECT * FROM home WHERE four like ?", [
sell
] ,function(err , rows){
if (err) { return next(err);}
var homes = rows.map(function(row) {
return {
id: row.id,
location: row.Location,
four: row.four,
space: row.space,
bathrooms: row.bathrooms,
bedrooms: row.bedrooms,
price: row.price,
url: '/' + row.id
}
});
res.locals.homes = homes;
next();
});
};
这是我按下按钮时请求的路由:
router.get('/buyHome', ensureLoggedIn ,fetchData,findHomeforSell , findHomeforRent, (req , res , next) => {
res.render('index', {user: req.user ,namor: res.locals.homes, rent: res.locals.rent});
next();
});
这是 html 表格:
<form action="/buyHome" method="get">
<ul class="todo-list">
<% namor.forEach(function(namor) { %>
<li>
<div class="view" id="view">
<table>
<tr>
<th></th>
<th>Location</th>
<th>For</th>
<th>Space</th>
<th>Bathroom</th>
<th>Bedroom</th>
<th>Price</th>
</tr>
<tr>
<td rowspan="2"><img src="https://picsum.photos/200" id="circul" alt=""></td>
<td><%= namor.location %></td>
<td><%= namor.four %></td>
<td><%= namor.space %> SQRM</td>
<td><%= namor.bathrooms %></td>
<td><%= namor.bedrooms %> </td>
<td>$ <%= namor.price %> SDG</td>
</tr>
</table>
</div>
</li>
<% }); %>
</ul>
<input type="button" value="GO">
</form>
我的问题是在调用它或按下按钮之前出现数据
i am working on a project using express , EJS and SQLite, so i made search operation function that fetch data from the database, i want to display the data when i press the button, not before it, this is the function i made :
function findHomeforSell(req , res , next){
const sell = "sell";
db.all("SELECT * FROM home WHERE four like ?", [
sell
] ,function(err , rows){
if (err) { return next(err);}
var homes = rows.map(function(row) {
return {
id: row.id,
location: row.Location,
four: row.four,
space: row.space,
bathrooms: row.bathrooms,
bedrooms: row.bedrooms,
price: row.price,
url: '/' + row.id
}
});
res.locals.homes = homes;
next();
});
};
This is the route that requested when i press the button:
router.get('/buyHome', ensureLoggedIn ,fetchData,findHomeforSell , findHomeforRent, (req , res , next) => {
res.render('index', {user: req.user ,namor: res.locals.homes, rent: res.locals.rent});
next();
});
This is HTML form :
<form action="/buyHome" method="get">
<ul class="todo-list">
<% namor.forEach(function(namor) { %>
<li>
<div class="view" id="view">
<table>
<tr>
<th></th>
<th>Location</th>
<th>For</th>
<th>Space</th>
<th>Bathroom</th>
<th>Bedroom</th>
<th>Price</th>
</tr>
<tr>
<td rowspan="2"><img src="https://picsum.photos/200" id="circul" alt=""></td>
<td><%= namor.location %></td>
<td><%= namor.four %></td>
<td><%= namor.space %> SQRM</td>
<td><%= namor.bathrooms %></td>
<td><%= namor.bedrooms %> </td>
<td>$ <%= namor.price %> SDG</td>
</tr>
</table>
</div>
</li>
<% }); %>
</ul>
<input type="button" value="GO">
</form>
My problem is that the data appear before i call it or press the button
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论