Node.js 从同一数据库mongoose、ejs中的另一个集合获取数据
我制作了一个web应用程序,它使用Firebase进行存储,使用MongoDB atlas作为数据库。我制作了一个模式来存储一个部分的文件路径和标题。然后,我还向我的应用程序添加了另一个模式,以便在同一数据库中创建另一个集合。现在我面临的主要问题是无法从第二个集合中检索数据到index.ejs页面。这是我的密码:Node.js 从同一数据库mongoose、ejs中的另一个集合获取数据,node.js,mongodb,mongoose,ejs,mongoose-schema,Node.js,Mongodb,Mongoose,Ejs,Mongoose Schema,我制作了一个web应用程序,它使用Firebase进行存储,使用MongoDB atlas作为数据库。我制作了一个模式来存储一个部分的文件路径和标题。然后,我还向我的应用程序添加了另一个模式,以便在同一数据库中创建另一个集合。现在我面临的主要问题是无法从第二个集合中检索数据到index.ejs页面。这是我的密码: //MongoDB init mongoose.connect( "mongodb+srv://<My_DB>:<MY_DB_pass>@clus
//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);
EJS代码:
此代码呈现我的第一个集合中的数据
第二个EJS代码,我试图在其中呈现来自第二个集合的数据
更改路由功能以查询其他集合:
app.get("/", function (req, res) {
User.find()
.then(newListItems => {
Test.find() // <- Your other collection
.then(testListItems => {
res.render("index", { newListItems, testListItems });
})
})
});
app.get(“/”,函数(req,res){
User.find()
。然后(newListItems=>{
Test.find()//{
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中有
。这与没有secondPodcastTitle
的架构不一致。它有secondTitle
,因此我将ejs更改为:
两个ejs块是否位于同一个文件中?它们都出现在索引中吗?@codemonkey是的,两个块都在同一个文件index.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>
<% }) %>
app.get("/", function (req, res) {
User.find()
.then(newListItems => {
Test.find() // <- Your other collection
.then(testListItems => {
res.render("index", { newListItems, testListItems });
})
})
});
<% 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>
<% }) %>