ExpressJS:我想从数据库获取数据

发布于 2025-02-12 15:30:45 字数 3182 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文