繁体   English   中英

从同一数据库中的另一个集合中获取数据 mongoose,ejs

[英]Get data from another collection in same database mongoose, ejs

我制作了一个 web 应用程序,该应用程序使用 Firebase 进行存储,并使用 MongoDB 地图集作为数据库。 我制作了一个模式,用于存储一个部分的文件路径和标题。 然后我还在我的应用程序中添加了另一个模式,以便在同一个数据库中创建另一个集合。 现在我面临的主要问题是我无法将第二个集合中的数据检索到我的 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>
                    <% }) %>

更改您的路线 function 以查询其他集合:

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 中有<%=item.secondPodcastTitle%> 这与没有secondPodcastTitle的架构不一致。 它有secondTitle ,所以我将 ejs 更改为: <%=item.secondTitle%>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM