搜索结果页面未加载任何数据。

huangapple go评论68阅读模式
英文:

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: [&quot;tv&quot;, &quot;movies&quot;, &quot;music&quot;],
},
});

Inside my catalog route page I have this GET request for when a user loads a search results page.

router.get(&quot;/article/search/:id&quot;, 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({}, &quot;title&quot;)
.sort({ title: 1 })
.exec(function (err, list_articles) {
if (err) {
return next(err);
}
res.render(&quot;search&quot;, {
article_list: list_articles,
id: id,
});
});
console.log(id);
};

Inside my search ejs page I have this code

&lt;% article_list.forEach(function(article) { %&gt;
&lt;% if (article.category === id) { %&gt;
&lt;div class=&quot;article&quot;&gt; 
&lt;p class=&quot;title&quot;&gt; &lt;a href=&lt;%= article.url %&gt;&gt;&lt;%= article.title %&gt; &lt;/p&gt;&lt;/a&gt;
&lt;p class=&quot;category&quot;&gt; &lt;%= article.category %&gt; &lt;/p&gt;
&lt;p class=&quot;author&quot;&gt; written by: &lt;%= article.author %&gt; &lt;/p&gt;
&lt;/div&gt;
&lt;% } else { %&gt;
&lt;p&gt;No Articles Match This Query&lt;/p&gt;
&lt;% } %&gt;
&lt;% }); %&gt;

And finally, on my index ejs I have these a links.

&lt;li&gt;&lt;a href=&quot;/catalog/article/search/tv&quot;&gt;TV&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/catalog/article/search/movies&quot;&gt;Movies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/catalog/article/search/music&quot;&gt;Music&lt;/a&gt;&lt;/li&gt;

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: &quot;title&quot;})

huangapple
  • 本文由 发表于 2023年4月19日 16:37:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定