【问题标题】:Why can't I reach the names from the DB?为什么我无法从数据库中获取名称?
【发布时间】:2020-11-26 23:50:56
【问题描述】:

//user.js

我用猫鼬创建了以下模型架构:

var UserSchema = new mongoose.Schema ({
    username: String,
    password: String,
    ThisSetup: [{name: String, quote: String}]
});
module.exports = mongoose.model("User", UserSchema);

在 mongoDB 中,我可以在终端中看到,这个概念运行良好,但是如果我尝试使用 ejs 显示 name 属性,则会显示 id,但名称不会:

//userid.ejs

<% User.forEach(function(User){ %>
    <div class="row">
        <p><%= User._id %>   <%= User.ThisSetup.name%></p>
    </div>
<% }); %

这样我也可以在浏览器中看到名称和引号:

<%= User.ThisSetup %>

我怎样才能只找到名字?

提前感谢您

【问题讨论】:

  • 试试这个 - &lt;% User.ThisSetup.forEach(function(User){ %&gt; &lt;div class="row"&gt; &lt;p&gt;&lt;%= User._id %&gt; &lt;%= User.name%&gt;&lt;/p&gt; &lt;/div&gt; &lt;% }); %
  • @KunalMukherjee 我试过了,但也不行
  • ThisSetup 被定义为一个数组,所以你需要指定你想要哪个元素的名字。

标签: javascript mongodb mongoose ejs


【解决方案1】:

谢谢!

解决办法:

<%= User.thisSetup[0].name %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多