編碼新聞/媒體網站,我希望有一個「新聞」欄目,「評論」部分, 「趨勢」部分,它結合了前面的章節中,就像這裏: 我已經一個用於「新聞」的模式,一個用於「評論」。如何製作「趨勢」部分(如「電影」部分上方的圖片)? 代碼: 在app.js,MongoDB的架構定義
//LANDING PAGE
app.get('/', function (req, res,next) {
Blogdemo.find({}).sort([['_id', -1]]).limit(3).exec(function(err,allBlogs) { //finds latest posts for 1st Schema (upto 3)
if(err) {
console.log(err);
next();
} else {
res.locals.blog = allBlogs;
// res.render("landing", {blog : allBlogs , moment : now});
next();
}
})
}, function (req, res) {
Review.find({}).sort([['_id', -1]]).limit(3).exec(function(err,allReviews) { //finds latest posts of 2nd Schema
if(err) {
console.log(err);
} else {
res.locals.review = allReviews;
res.render("landing", res.locals);
}
})
})
在review.js,
var mongoose = require("mongoose");
//SCHEMA SETUP
var reviewSchema = new mongoose.Schema({
image : String,
title : String,
body : String,
rating : String,
created : {type : Date, default : Date.now()},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment" //name of the model
}
]
})
module.exports = mongoose.model("review", reviewSchema);
「新聞」 的模式幾乎是一樣的(沒有檢討)。 我的方式定義架構錯誤?如果不是,那麼我如何構建「趨勢」部分?
是否有任何MongoDB方法可以從「新聞」和「評論」中找到最新的帖子來構建「趨勢」部分(就像第一張圖片一樣)?
您是否定義了趨勢科的模式? –
另外,你能告訴我審查和新聞的模式,以便我可以幫助你與趨勢部分。 –
我還沒有爲趨勢部分定義架構。我已經更新了問題中的代碼。 – eknoor4197