【问题标题】:Difficulties with displaying post information on seperate page在单独的页面上显示帖子信息的困难
【发布时间】:2017-12-09 08:27:15
【问题描述】:

我正在制作一个网站,您可以在上面发布内容,并且帖子会显示在您的提要中,有点像 instagram。一切都可以从数据库中检索帖子并使用 forEach 循环将它们全部显示在一个页面上。有一个按钮,上面写着“更多详细信息”,当点击它时,你会进入一个单独的页面,上面只有那个帖子。 问题是当我尝试转到该页面时,什么都没有显示。也没有显示错误,并且显示了其他标题信息。

app.get("/posts/:author/:id", function(req, res){
    Post.findAll({
        where: {
            id: req.params.id,
            author: req.params.author
        }
    }).then(function(foundPost){
        var renderPost = foundPost;
        res.render("show", {post: renderPost});
    });
});

那是页面get请求的代码。^

<% include ./partials/header %>

<h1><%= post.author%></h1>
<h1><%= post.body%></h1>
<h1><%= post.title%></h1>

hi

<% include ./partials/footer %>

这是实际显示页面的代码。 当您使用检查时,“hi”会与 3 个空的 h1 一起显示。

【问题讨论】:

  • 您使用什么模块进行数据库查询?
  • 我正在使用续集。

标签: html mysql node.js


【解决方案1】:

我假设findAll 方法返回一个Array,所以为了得到post 对象,你应该这样做:

var renderPost = foundPost[0];

【讨论】:

  • 一开始觉得很有道理,但是做了之后,“更多详情”页面只显示了发帖人的名字。
  • 查看我的“更多详细信息”页面的代码后,我意识到 post.title 和 post.body 与数据库中的列名不匹配。我确实在 foundPost 上使用了“[0]”,它确实帮助了我。感谢您的帮助。
猜你喜欢
  • 2011-12-30
  • 1970-01-01
  • 2020-01-25
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-20
相关资源
最近更新 更多