首页 > 解决方案 > Express js/Handlebars 新手 - 使用 res.locals.currentPost 仅在某些情况下有效,无法弄清楚原因

问题描述

这是我第一次在堆栈溢出上发帖,我会尽力描述我的问题。

我正在开发一个图像网站项目(想想 imgur/flickr),并且第一次使用车把模板。我制作了一个页面来查看带有照片信息(发布者和描述)和评论部分的个人照片。评论部分完美运行,但无法加载照片和照片信息。

我已经尝试了几个小时,但我无法弄清楚为什么它只适用于一个而不适用于另一个。我编写了错误代码来跟踪信息,它确实到达了页面所需的中间件,但没有出现在页面上。

这是我的中间件页面代码:

var {getMRecentPosts, getMPostById} = require('../models/Posts');
const {getCommentsForPost} = require('../models/Comments');
const { errorPrint, successPrint } = require('../helpers/debug/debugprinters');
const postMW = {};

postMW.getPostById = async function (req, res, next) {
    try{
        successPrint(req.params.id);
        let results =  await getMPostById(req.params.id);

        successPrint(results[0]);

        if(results && results.length){
            res.locals.currentPost = results[0];
            errorPrint(res.locals.currentPost);
            next();
        }else{
            req.flash("error", "This is not the post you are looking for.");
            res.redirect('/');
        }   
    }catch(error){
        next(error);
    }
}

postMW.getCommentsByPostId = async function (req, res, next) {
    try{
        let results = await getCommentsForPost(req.params.id);
        res.locals.currentPost.comments = results;
        next();
    }catch (error){
        next(error);
    }
}

module.exports = postMW;

这是来自我的 errorPrinter 的控制台消息(再次,没有实际的系统错误只是试图确保信息到此为止):

[
  BinaryRow {
    username: 'dan',
    title: 'Flying fox back at it again',
    description: 'Once a gain not actually a fox, just called on',
    photopath: 'public\\images\\uploads\\3404ba6eb264c819ff7011378b756c8426c17e4d57bc.jpeg',
    created: 2020-12-20T00:28:28.000Z
  }
]

这是我的 Hbs 文件代码:

<div id="post-container">
    <div id="photo-container">
        <div id="post-title" class="display-6 font-weight-bold">{{currentPost.title}}</div>
        <div id="post-info">
            <p><span class="form-label">Posted by:</span>
                <span id="post-author">
                    {{currentPost.username}}
                </p>
            <p><span class="form-label">Posted at:</span><span id="post-date">{{currentPost.created}}</p>
        </div>
        <div id="post-description" class="lead">{{currentPost.description}}</div>
        <img id="post-image" class="img-fluid" src="/{{currentPost.photopath}}" alt="A photo should have been here">
    </div>

    <div id="comment-container">
        <div id="messages">
            {{#each currentPost.comments}}
            {{> comment this}}
            {{/each}}
        </div>
        <div id="comment-box">
            <textarea id="comment-box-text" class="form-control" aria-label="With textarea"
                placeholder="Enter Comment Here!"></textarea>
            <span id="comment-box-button" class="input-group-text"><img src="https://img.icons8.com/metro/26/ffffff/paper-plane.png"/></span>
        </div>
    </div>
</div>

欢迎任何见解!我对错误是什么一无所知和/或我真的不擅长在谷歌上搜索正确的帮助,哈哈

标签: javascripthtmlexpresshandlebars.jsexpress-handlebars

解决方案


推荐阅读