【问题标题】:Get data from another collection in same database mongoose, ejs从同一数据库mongoose,ejs中的另一个集合中获取数据
【发布时间】:2021-05-24 23:14:20
【问题描述】:

我制作了一个使用 Firebase 进行存储并使用 MongoDB atlas 作为数据库的 Web 应用程序。我制作了一个模式,用于存储一个部分的文件路径和标题。然后我还在我的应用程序中添加了另一个模式,以便在同一个数据库中创建另一个集合。现在我面临的主要问题是我无法将第二个集合中的数据检索到我的 index.ejs 页面。这是我的代码:

//MongoDB init
mongoose.connect(
  "mongodb+srv://<My_DB>:<MY_DB_pass>@cluster0.cqqda.mongodb.net/proDB?retryWrites=true&w=majority"
);

mongoose.set("useCreateIndex", true);

const connectionParams = {
  useNewUrlParser: true,
  useCreateIndex: true,
  useUnifiedTopology: true,
};

mongoose
  .connect(url, connectionParams)
  .then(() => {
    console.log("Connected to database ");
  })
  .catch((err) => {
    console.error(`Error connecting to the database. \n${err}`);
  });

mongoose.set("useCreateIndex", true);
const dbName = "proDB";

const userSchema = mongoose.Schema({
  title: String,
  filepath: String,
});

const testSchema = mongoose.Schema({
  secondTitle: String,
  secondFilePath: String,
});

testSchema.plugin(findOrCreate);

userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);

//DATABASE MODEL
const User = new mongoose.model("User", userSchema);
const Test = new mongoose.model("Test", testSchema);

索引路线:

app.get("/", function (req, res) {
  User.find({}, function (err, foundItems) {
    // console.log(foundItems);
    res.render("index", { newListItems: foundItems });
  });
});

EJS 代码: 此代码呈现我的第一个集合中的数据

<% newListItems.forEach(function(item){ %>
                        <div class="col-xs-6 col-sm-4 video-div " >
                            <!-- before col-sm -->
                            <div class="main-video">
                                <video style="outline: none; width: 100%; height: 200px;" 
                                    width="auto" 
                                   height="220" controls>
                                    <source src="<%=item.filepath%>" type="video/mp4">
                                </video>
                            </div>
                            <div class="video-title" style="width: 50%;">
                                <p class="podcast-title">
                                    <%=item.title%>
                                </p>
                            </div>
                        </div>
                        <% }) %>

第二个 EJS 代码,我试图在其中呈现我的第二个集合中的数据

<% newListItems.forEach(function(item){ %>
                    <div class="col-xs-6 col-sm-4 video-div ">
                        <!-- before col-sm -->
                        <div class="main-video">
                            <video style="outline: none; width: 100%; height: 200px;" width="auto" 
                                height="220"
                                controls>
                                <source src="<%=item.secondFilePath%>" type="video/mp4">
                            </video>
                            <!-- <img style="width: 100%; height: auto;" src="" alt=""> -->
                        </div>
                        <div class="video-title" style="width: 50%;">
                            <p class="podcast-title">
                                <%=item.secondPodcastTitle%>
                            </p>
                        </div>
                    </div>
                    <% }) %>

【问题讨论】:

  • 两个 EJS 块是否存在于同一个文件中?他们都出现在index吗?
  • @codemonkey 是的,这两个块都在同一个文件 index.ejs 中

标签: node.js mongodb mongoose ejs mongoose-schema


【解决方案1】:

更改您的路由函数以查询其他集合:

app.get("/", function (req, res) {
  User.find()
    .then(newListItems => {
      Test.find() // <- Your other collection
        .then(testListItems => {
          res.render("index", { newListItems, testListItems });
        })
    })
});

然后在你的 EJS 中,这样做:

                     <% newListItems.forEach(function(item){ %>
                        <div class="col-xs-6 col-sm-4 video-div " >
                            <!-- before col-sm -->
                            <div class="main-video">
                                <video style="outline: none; width: 100%; height: 200px;" 
                                    width="auto" 
                                   height="220" controls>
                                    <source src="<%=item.filepath%>" type="video/mp4">
                                </video>
                            </div>
                            <div class="video-title" style="width: 50%;">
                                <p class="podcast-title">
                                    <%=item.title%>
                                </p>
                            </div>
                        </div>
                     <% }) %>
...
...
                 <% testListItems.forEach(function(item){ %>
                    <div class="col-xs-6 col-sm-4 video-div ">
                        <!-- before col-sm -->
                        <div class="main-video">
                            <video style="outline: none; width: 100%; height: 200px;" width="auto" 
                                height="220"
                                controls>
                                <source src="<%=item.secondFilePath%>" type="video/mp4">
                            </video>
                            <!-- <img style="width: 100%; height: auto;" src="" alt=""> -->
                        </div>
                        <div class="video-title" style="width: 50%;">
                            <p class="podcast-title">
                                <%=item.secondTitle%>
                            </p>
                        </div>
                    </div>
                  <% }) %>

请注意,您原来的 ejs 中有 &lt;%=item.secondPodcastTitle%&gt;。这与您没有secondPodcastTitle 的架构不一致。它有secondTitle,所以我将ejs更改为:&lt;%=item.secondTitle%&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2016-06-20
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多