【问题标题】:Nesting {{#each}} in Handlebars 304 error在 Handlebars 304 错误中嵌套 {{#each}}
【发布时间】:2023-03-07 13:08:01
【问题描述】:

我的嵌套 {{#each}} 不起作用。 我正在将文章标题刮到我的页面上,并且正在使用 MongoDB/Mongoose。对于每篇文章(我命名为 News),我希望用户发布 cmets 并查看以前的 cmets。 我的路由、console.log 和数据库本身显示我的 api 路由正在工作,但我无法让 cmets 显示。 我的车把将文章渲染到屏幕上,但是在获取 cmets 时它返回 304。

文章按我的意图显示,只有 cmets 有问题 {{#each News}} 是每篇文章 {{#each comment}} 嵌套在每个 News 中。

{{#each News}}
        <div class="panel-block is-active">
          <ul>
            <li>
              <div class="level-left">

                <h2  class="subtitle">
                  <a href="https://www.atptour.com{{this.url}}">
                  {{this.headline}}
                  </a>
                </h2>
              </div>
            </li>
            <li>
             <img width="348" src="https://www.atptour.com{{this.thumbNail}}">

            </li>
            <li>
             <p>{{category}}</p>
            </li>
            <li>
                {{!-- on click remove class .is-hidden on #getComments --}}
              <p id="showComments" data-id="{{this._id}}" >Comments</p>
            </li>
            <li>
              {{#each comment}}
              <article id="getComments" class="media is-hidden">
                <div class="media-content">
                  <div class="content">
                    <p>
                      <small>{{this.time}}</small>
                      <br />
                      {{this.body}}
                    </p>
                  </div>
                </div>
                <div class="media-right">
                  <button class="delete"></button>
                </div>
              </article>
              {{/each}}
              {{!-- on click remove class .is-hidden on #postComments --}}
              <a id="addComment" >Add Comment</a>
              {{!-- TO POST A COMMENT --}}
              <article id="postComment" class="media is-hidden">
                <div class="media-content">
                  <div class="field">
                    <p class="control">
                      <textarea
                        class="textarea"
                        placeholder="Add a comment..."
                      ></textarea>
                    </p>
                  </div>
                  <div class="level">
                    <div class="level-left">
                      <div class="level-item">

                        <a class="button is-info">Submit</a>
                      </div>
                    </div>
                  </div>
                </div>
              </article>
            </li>
          </ul>
        </div>
        {{/each}}

在 {{#each 评论}} {{this.time}} 和 {{this.body}} 是空白的。但是,如果我简单地输入 {{this}},我会在每条评论中得到 _id。上下文一定是错误的,但我不知道为什么。 cmets 与 News 正确关联,因为我可以检查 get 路线并且一切都正确。

如果有帮助,这里是模式

new articles
const NewsSchema = new Schema({
  headline: {
    type: String,
    required: true,
  },
  thumbNail: {
    type: String,
    required: true,
  },
  category: {
    type: String,
    required: true,
  },

  url: {
    type: String,
    required: true,
  },

  // Comments will populate the News
  comment: [
    {
      type: Schema.Types.ObjectId,
      ref: "Comment",
    },
  ],
});
const News = mongoose.model("News", NewsSchema);

cmets

const CommentSchema = new Schema({
  time: {
    type: Date,
    default: Date.now,
    required: true,
  },

  body: {
    type: String,
    required: true,
  },
});
const Comment = mongoose.model("Comment", CommentSchema);

【问题讨论】:

    标签: mongodb nested handlebars.js each express-handlebars


    【解决方案1】:

    这根本不是车把问题! 这是一个路线问题。

    我正在这样做:

    app.get("/", function(req, res) {
    db.News.find()
      .then(News => {
        res.render("index", { News });
      })
      .catch(err => {
        console.log(err);
      });
    

    });

    什么时候我应该这样做

    app.get("/", function(req, res) {
    db.News.find()
      .populate("comment")
      .then(News => {
        res.render("index", { News });
      })
      .catch(err => {
        console.log(err);
      });
    

    });

    我忘记填充 res.render。 (我正在填充 api 路由,但不是这个 1!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-15
      • 2018-07-29
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 2019-06-13
      • 1970-01-01
      相关资源
      最近更新 更多