【问题标题】:How to retrieve data from mongodb and display in ejs page?如何从 mongodb 中检索数据并显示在 ejs 页面中?
【发布时间】:2017-05-10 22:48:11
【问题描述】:

我已经向 mongodb 添加了数据。我正在使用猫鼬。我想从 mongodb 数据库中获取数据并将其显示在 ejs 页面中。我在互联网上搜索了很多,但在尝试这些时我遇到了错误。我不明白错误是什么。这是我最近尝试的。

猫鼬模式

 var mongoose = require("mongoose");

 var Schema = mongoose.Schema;

 var UserSchema = new Schema({

     username:String,
     _id:String,
     dateOfBirth:Date,
     telephoneNo:Number,
     address:String,
     email:String,
     fb_Id:String,
     jobTitle:String,
     password:String
 });

 module.exports=mongoose.model('User',UserSchema);

我从 mongodb 获取数据的地方

router.get('/', function(req, res){
user.find({}, function(err, profile){
    res.render('AdminDatabase', { profile : profile});
   });
});

我在 ejs 页面中的显示位置

<div>
    <ul>
    <% for(var i=0; i<profile.length; i++) {%>
        <li><%= profile[i].username %></li>
        <li><%= profile[i]._id %></li>
        <li><%= profile[i].dateOfBirth %></li>
        <li><%= profile[i].telephoneNo %></li>
        <li><%= profile[i].address %></li>
        <li><%= profile[i].email %></li>
        <li><%= profile[i].fb_Id %></li>
        <li><%= profile[i].jobTitle %></li>
    <% } %>
    </ul>
</div>

但是当我刷新 AdminDatabase 页面时,我收到错误提示

ReferenceError: E:/Software project/Project/myProject/views/AdminDatabase.ejs:67

配置文件未定义

【问题讨论】:

  • 您的数据库中有一个名为users 的集合,对吗? show collections
  • 是的,我有一个收藏。我没有得到你。您需要查看收藏吗?

标签: node.js mongodb express mongoose


【解决方案1】:

'User' 不会变成'users',所以查找代码应该是这样的

users.find({}...

..?

【讨论】:

  • 请提供带解释的格式化代码答案。 SO 不像大多数论坛那样工作,您可以在其中发表评论以启动讨论并最终获得答案,它期望问题得到可靠/完整的答案。
猜你喜欢
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 2022-01-24
  • 2020-12-18
  • 2019-10-05
  • 1970-01-01
相关资源
最近更新 更多