英文:
My search results page isn't loading any data
问题
我理解你的需求是要翻译代码部分,以下是代码的翻译:
// Article Schema
const ArticleSchema = new Schema({
title: { type: String, required: true },
content: { type: String, required: true },
author: { type: String },
createdAt: { type: Date, default: Date.now },
category: {
type: String,
enum: ["tv", "movies", "music"],
},
});
// Catalog route GET request
router.get("/article/search/:id", article_controller.search);
// Search function
exports.search = async function (req, res, next) {
const id = req.params.id;
Article.find({}, "title")
.sort({ title: 1 })
.exec(function (err, list_articles) {
if (err) {
return next(err);
}
res.render("search", {
article_list: list_articles,
id: id,
});
});
console.log(id);
};
// EJS code in the search page
<% article_list.forEach(function(article) { %>
<% if (article.category === id) { %>
<div class="article">
<p class="title"> <a href=<%= article.url %>>><%= article.title %></p></a>
<p class="category"> <%= article.category %> </p>
<p class="author"> written by: <%= article.author %> </p>
</div>
<% } else { %>
<p>No Articles Match This Query</p>
<% } %>
<% }); %>
// Links in the index page
<li><a href="/catalog/article/search/tv">TV</a></li>
<li><a href="/catalog/article/search/movies">Movies</a></li>
<li><a href="/catalog/article/search/music">Music</a></li>
请注意,以上是代码的翻译部分,没有包含问题或其他内容。如果你需要进一步的帮助或解释,请随时提问。
英文:
I'm working on a blog and using Express and ejs to render the page. I have an Article Schema that has a category key that lists random values.
const ArticleSchema = new Schema({
title: { type: String, required: true },
content: { type: String, required: true },
author: { type: String },
createdAt: { type: Date, default: Date.now },
category: {
type: String,
enum: ["tv", "movies", "music"],
},
});
Inside my catalog route page I have this GET request for when a user loads a search results page.
router.get("/article/search/:id", article_controller.search);
And this is the function attached to the GET request.
exports.search = async function (req, res, next) {
const id = req.params.id;
Article.find({}, "title")
.sort({ title: 1 })
.exec(function (err, list_articles) {
if (err) {
return next(err);
}
res.render("search", {
article_list: list_articles,
id: id,
});
});
console.log(id);
};
Inside my search ejs page I have this code
<% article_list.forEach(function(article) { %>
<% if (article.category === id) { %>
<div class="article">
<p class="title"> <a href=<%= article.url %>><%= article.title %> </p></a>
<p class="category"> <%= article.category %> </p>
<p class="author"> written by: <%= article.author %> </p>
</div>
<% } else { %>
<p>No Articles Match This Query</p>
<% } %>
<% }); %>
And finally, on my index ejs I have these a
links.
<li><a href="/catalog/article/search/tv">TV</a></li>
<li><a href="/catalog/article/search/movies">Movies</a></li>
<li><a href="/catalog/article/search/music">Music</a></li>
Using console.log
I've determined the req.params.id
is properly rendering the value to whichever a
link I'm clicking on. Ex: movies
.
What I'm trying to do
What I've been doing is creating random articles and adding in a proper category. What I'm trying to do now is have a search page that only shows the articles with the category of whichever a
link I clicked on from the home page. So if I select movies from the home page, I want the search page to only show movies with the category of movies
. Right now I have about 20 articles posted in my database. The only thing that shows up when I click on one of the a
links is the header, sidebar and the else
code repeated 20 times.
No Articles Match This Query
No Articles Match This Query
No Articles Match This Query
etc...
答案1
得分: 1
你需要指定要查找的字段。
所以如果你想在标题字段上查找,你需要像这样使用:
Article.find({title: "标题"})
英文:
You need to specify on which field you want to find.
So if you want to find on title field, you need to use like this:
Article.find({title: "title"})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论