Node.js 如何在html中显示mongoDB集合?
我是mongoose的初学者,希望在基本html列表中的名为“example.ejs”的文件中显示“exColl”集合中的mongoDB文档,但是我遇到了各种问题。还有其他关于这个话题的帖子,但我仍然对此感到困惑 -我确实有一个工作代码块,它使用res.json从exColl.find({})输出所有文档,显然是以json格式输出的。但是,我无法将这段代码改编成使用res.render(例如)工作的代码 -当我在app.js中定义一个变量并尝试在example.ejs中访问它时,找不到该变量,因此即使我可以将exColl.find({})的结果保存在一个变量中,我也不知道如何将其输入HTML 很明显,我不知道什么是我不知道的,这是非常令人沮丧的。如果有人能帮我填补概念上的空白,那就太棒了 ---编辑---- 添加我尝试过的代码片段Node.js 如何在html中显示mongoDB集合?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我是mongoose的初学者,希望在基本html列表中的名为“example.ejs”的文件中显示“exColl”集合中的mongoDB文档,但是我遇到了各种问题。还有其他关于这个话题的帖子,但我仍然对此感到困惑 -我确实有一个工作代码块,它使用res.json从exColl.find({})输出所有文档,显然是以json格式输出的。但是,我无法将这段代码改编成使用res.render(例如)工作的代码 -当我在app.js中定义一个变量并尝试在example.ejs中访问它时,找不到该变量,因
app.get("/example", function (req, res){
exColl.find({})
.exec(function (err, examples){
if (err) {
res.send("an error has occurred")
} else res.render(examples: examples);
});
});
在.ejs文件中
<p> <%= examples %> </p>
您的问题似乎是您应该在此处查看的EJS语法:。考虑下面的测试项目结构:
.
├── index.js
├── package.json
├── setup.js
└── views
├── index.ejs
└── table.ejs
我使用setup.js创建了一个测试数据库,这样我们就可以显示一些虚拟帖子:
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:8081/test", {
useNewUrlParser: true
});
const Post = mongoose.model("Post", {
title:String,
body: String
});
const toMake = [
{title: "hello", body: "world"},
{title: "foo", body: "bar"},
{title: "fizz", body: "buzz"},
{title: "a", body: "b"}
];
Post.insertMany(toMake)
.then(()=>{
console.log("done");
mongoose.connection.close();
})
.catch(err => console.error(err));
我创建一个EJS模板视图/表格。EJS将我的帖子呈现为表格:
<table>
<thead>
<tr>
<th>Title</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<% posts.forEach(post => { %>
<tr>
<td><%= post.title %></td>
<td><%= post.body %></td>
</tr>
<% }) %>
</tbody>
</table>
当我curl localhost:3000
时,我得到呈现的HTML:
<main>
<h1>Posts</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<tr>
<td>hello</td>
<td>world</td>
</tr>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
<tr>
<td>fizz</td>
<td>buzz</td>
</tr>
<tr>
<td>a</td>
<td>b</td>
</tr>
</tbody>
</table>
</main>
我在/sliced
向我的应用程序添加了另一条路由:
app.get("/sliced", async (req, res) => {
const posts = await Post.find({});
res.render("profile", {posts});
});
每当我curllocalhost:3000/sliced
时,我只会得到帖子中的前两项,因为我只用所有帖子的一部分填充了include
的作用域:
<main>
<h1>2 Posts</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<tr>
<td>hello</td>
<td>world</td>
</tr>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
</tbody>
</table>
</main>
2个员额
标题
身体
你好
世界
福
酒吧
您试图如何渲染此内容?使用Pug/Jade/其他一些模板引擎?我的视图引擎是ejsCan,您可以演示如何调用res.render()
?尽可能多地发布路由处理程序中的相关代码以及ejs
文件中的相关代码。@zero298编辑了帖子谢谢@zero298,此代码有效!除了.esj问题之外,我认为我还误用了模式。如何才能在不干扰get方法的情况下将此表放置在主页以外的页面中?@HenryPopiolek仅为该表制作另一个EJS模板,并使用
请注意,尽管您仍需要像在我的回答示例中所做的那样,在呈现范围中提供tableData
。链接文档还介绍了如何使用
@HenryPopiolek。请参阅我编辑的答案,了解更多详细信息。
<main>
<h1>2 Posts</h1>
<%- include("table", {posts: posts.slice(0, 2)}); %>
</main>
app.get("/sliced", async (req, res) => {
const posts = await Post.find({});
res.render("profile", {posts});
});
<main>
<h1>2 Posts</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<tr>
<td>hello</td>
<td>world</td>
</tr>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
</tbody>
</table>
</main>