【问题标题】:Query data from mongoose and display it on a html page从 mongoose 查询数据并显示在 html 页面上
【发布时间】:2014-08-30 04:07:21
【问题描述】:

这是我的架构

var productSchema = new mongoose.Schema({
    name: String,
    description: String
});
var Product = mongoose.model('Product', productSchema);

在我使用的 index.js 中

exports.welcome = function(req,res) {
    Product.find({},{},function(err,docs) {
        res.render('welcome', {
            "productlist" : docs
        });
    });
};

在我的 app.js 中,我调用了这个语句,其中 routes 是我在 index.js 中调用 Welcome 的变量 app.get('/welcome',routes.welcome);

我的架构也是用 index.js 编写的。我想要做的是在名为“welcome.html”的 html 页面中显示所有产品及其名称和描述。

谁能告诉我我应该在我的 html 页面中写什么来做到这一点。

【问题讨论】:

  • 你用的是什么 html 渲染器?
  • app.set('views', path.join(__dirname, 'views')); app.engine('html', require('ejs').renderFile); app.set('视图引擎', 'html');这是我在 app.js 中写的。
  • 我能找到的任何地方都像将数据发送到翡翠然后显示在翡翠中但不是在html中?

标签: html node.js mongodb


【解决方案1】:

根据您的最新评论,这意味着您正在使用 EmbeddedJS 作为模板引擎。你的答案有据可查here

出于同谋,welcome.html 显示结果的示例是:

<!DOCTYPE html>
<html>
<head>
<title>My Products</title>
</head>

<body>
<ul>
<% for(var i=0; i<productlist.length; i++) {%>
   <li><%= productlist[i].name %> : <%= productlist[i].description %></li>
<% } %>
</ul>
</body>

</html>

【讨论】:

  • 欢迎来到 StackOverflow :)
  • 你能帮我做一件事吗?我想将数据从 html 页面发送到猫鼬。我已经通过使用发布请求创建表单来完成这件事,并且我可以将数据发送给猫鼬,但是如果我不想使用表单,我还能发送数据吗?如果您有示例请分享链接。
  • 您可以使用 AJAX en.wikipedia.org/wiki/Ajax_%28programming%29 将数据从客户端发送到服务器。有关更多信息,请搜索互联网或提出单独的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
相关资源
最近更新 更多