mongoose:find具有特定条件的文件

我正在编写一个新闻网站。 我为特定的作者/记者做了一个专门的页面。 此页面将包含他/她的所有post。这里是代码:

在app.js中,

app.get("/author/:id", function(req,res) { //latest contains all posts Latest.find({author:"Richard Mann"}).sort([['_id', -1]]).exec(function(err,allLatest) { if(err) { console.log(err); } else { res.render("showAuthor", { latest : allLatest}); } }) }) 

此代码正常工作,并显示特定作者的post。 但是,如何为所有作者做到这一点(同时避免DRY代码)? 在find指定作者的文档时,我应该申请什么条件?

只要从客户端的input,并通过它find({author:req.params.id}这将检查您将从您的API /作者/ ID的ID的基础上,并使您的search任何指定的作者

 app.get("/author/:id", function(req,res) { //latest contains all posts Latest.find({author:req.params.id}).sort([['_id', -1]]).exec(function(err,allLatest) { if(err) { console.log(err); } else { res.render("showAuthor", { latest : allLatest}); } }) })