如何从SQLITE数据库中导入的循环数据
我正在构建一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您混合了变量,请尝试以下方法:
Looks like you mixed up variables, try this: