如何从SQLITE数据库中导入的循环数据

发布于 2025-02-10 04:55:32 字数 3359 浏览 2 评论 0原文

我正在构建一个 express 应用程序,并且我正在使用 sqlite 数据库,我设法从数据库中获取数据,但是我想在接口中显示它,所以我做到了:

index.js

/* GET THE LIST OF HOMES*/
router.get('/:homes-list', (req,res) => {
  db.all( "SELECT * FROM home WHERE owner_id = ?",[
    req.user.id
  ],
  function(err, rows) {
    if (err) { return next(err); }
    
    const home = rows.map(function(row) {
      return {
        location: row.location,
        for: row.for,
      }
        
    });
    res.locals.homes = home; 
    console.log(res.locals.homes)
    res.redirect("/:homes-list", {home: res.locals.homes})
  });
});

然后,我尝试使用 ejs:

index.ejs


                    <form action="/:homes-list" method="get">
                        <table>
                            <tbody>
                        <% home.forEach(function(homes) { %>
                            <tr>
                              <td><%= homes.location %></td>
                              <td><%= homes.for %></td> 
                            </tr>
                          <% }); %> 
                        </tbody>
                        </table>
                    </form>

在我所做的所有事情之后都 显示它错误:

错误

/media/onour/Desing/Des/test/roshan/views/index.ejs:45 43| <table> 44| <tbody> >> 45| <% home.forEach(function(homes) { %> 46| <tr> 47| <td><%= homes.location %></td> 48| <td><%= homes.for %></td> home is not defined
ReferenceError: /media/onour/Desing/Des/test/roshan/views/index.ejs:45
    43|                         <table>
    44|                             <tbody>
 >> 45|                         <% home.forEach(function(homes) { %>
    46|                             <tr>
    47|                               <td><%= homes.location %></td>
    48|                               <td><%= homes.for %></td> 

home is not defined
    at eval (eval at compile (/media/onour/Desing/Des/test/roshan/node_modules/ejs/lib/ejs.js:633:12), <anonymous>:11:8)
    at returnedFn (/media/onour/Desing/Des/test/roshan/node_modules/ejs/lib/ejs.js:668:17)
    at tryHandleCache (/media/onour/Desing/Des/test/roshan/node_modules/ejs/lib/ejs.js:254:36)
    at View.exports.renderFile [as engine] (/media/onour/Desing/Des/test/roshan/node_modules/ejs/lib/ejs.js:485:10)
    at View.render (/media/onour/Desing/Des/test/roshan/node_modules/express/lib/view.js:135:8)
    at tryRender (/media/onour/Desing/Des/test/roshan/node_modules/express/lib/application.js:640:10)
    at Function.render (/media/onour/Desing/Des/test/roshan/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/media/onour/Desing/Des/test/roshan/node_modules/express/lib/response.js:1008:7)
    at /media/onour/Desing/Des/test/roshan/routes/index.js:22:7
    at Layer.handle [as handle_request] (/media/onour/Desing/Des/test/roshan/node_modules/express/lib/router/layer.js:95:5)

,我不明白错误,所以我不知道该怎么办。

I am building an Express app ,and i am using SQLite database, I managed to fetch the data from the database, but I want to display it in the interface ,So i did this :

index.js

/* GET THE LIST OF HOMES*/
router.get('/:homes-list', (req,res) => {
  db.all( "SELECT * FROM home WHERE owner_id = ?",[
    req.user.id
  ],
  function(err, rows) {
    if (err) { return next(err); }
    
    const home = rows.map(function(row) {
      return {
        location: row.location,
        for: row.for,
      }
        
    });
    res.locals.homes = home; 
    console.log(res.locals.homes)
    res.redirect("/:homes-list", {home: res.locals.homes})
  });
});

Then I tried to display it for the user interface using EJS:

index.ejs


                    <form action="/:homes-list" method="get">
                        <table>
                            <tbody>
                        <% home.forEach(function(homes) { %>
                            <tr>
                              <td><%= homes.location %></td>
                              <td><%= homes.for %></td> 
                            </tr>
                          <% }); %> 
                        </tbody>
                        </table>
                    </form>

After i did all that i had an error:

Error

/media/onour/Desing/Des/test/roshan/views/index.ejs:45 43| <table> 44| <tbody> >> 45| <% home.forEach(function(homes) { %> 46| <tr> 47| <td><%= homes.location %></td> 48| <td><%= homes.for %></td> home is not defined
ReferenceError: /media/onour/Desing/Des/test/roshan/views/index.ejs:45
    43|                         <table>
    44|                             <tbody>
 >> 45|                         <% home.forEach(function(homes) { %>
    46|                             <tr>
    47|                               <td><%= homes.location %></td>
    48|                               <td><%= homes.for %></td> 

home is not defined
    at eval (eval at compile (/media/onour/Desing/Des/test/roshan/node_modules/ejs/lib/ejs.js:633:12), <anonymous>:11:8)
    at returnedFn (/media/onour/Desing/Des/test/roshan/node_modules/ejs/lib/ejs.js:668:17)
    at tryHandleCache (/media/onour/Desing/Des/test/roshan/node_modules/ejs/lib/ejs.js:254:36)
    at View.exports.renderFile [as engine] (/media/onour/Desing/Des/test/roshan/node_modules/ejs/lib/ejs.js:485:10)
    at View.render (/media/onour/Desing/Des/test/roshan/node_modules/express/lib/view.js:135:8)
    at tryRender (/media/onour/Desing/Des/test/roshan/node_modules/express/lib/application.js:640:10)
    at Function.render (/media/onour/Desing/Des/test/roshan/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/media/onour/Desing/Des/test/roshan/node_modules/express/lib/response.js:1008:7)
    at /media/onour/Desing/Des/test/roshan/routes/index.js:22:7
    at Layer.handle [as handle_request] (/media/onour/Desing/Des/test/roshan/node_modules/express/lib/router/layer.js:95:5)

and i didn't understand the error, so i didn't know what to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素食主义者 2025-02-17 04:55:32

看起来您混合了变量,请尝试以下方法:

<% homes.forEach(function(home) { %>
   <tr>
       <td><%= home.location %></td>
       <td><%= home.for %></td> 
   </tr>
<% }); %> 

Looks like you mixed up variables, try this:

<% homes.forEach(function(home) { %>
   <tr>
       <td><%= home.location %></td>
       <td><%= home.for %></td> 
   </tr>
<% }); %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文