簡體   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