【发布时间】: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