Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Express js/Handlebar新手-使用res.locals.currentPost仅在某些情况下有效,无法找出原因_Javascript_Html_Express_Handlebars.js_Express Handlebars - Fatal编程技术网

Javascript Express js/Handlebar新手-使用res.locals.currentPost仅在某些情况下有效,无法找出原因

Javascript Express js/Handlebar新手-使用res.locals.currentPost仅在某些情况下有效,无法找出原因,javascript,html,express,handlebars.js,express-handlebars,Javascript,Html,Express,Handlebars.js,Express Handlebars,这是我第一次发布关于堆栈溢出的帖子,我会尽力描述我的问题 我正在做一个图像网站项目(想想imgur/flickr),我第一次使用了把手模板。我制作了一个页面来查看个人照片,其中包含照片信息(发布者和描述)和评论部分。评论部分工作完美,但照片和照片信息无法加载 我已经试了几个小时,我不明白为什么它只对其中一个有效,而对另一个无效。我编写了错误代码来跟踪这些信息,它确实可以到达页面所需的中间件,但没有出现在页面上 以下是我的中间件页面代码: var {getMRecentPosts, getMPos

这是我第一次发布关于堆栈溢出的帖子,我会尽力描述我的问题

我正在做一个图像网站项目(想想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的控制台消息(再次强调,没有实际的系统错误,只是尝试确保信息能够达到这个程度):

以下是我的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>

{{currentPost.title}
投寄人:
{{currentPost.username}

发布地点:{{currentPost.created}

{{currentPost.description} {{{#每个currentPost.comments} {{>评论这个} {{/每个}}
欢迎有任何见解!!!我对错误是什么视而不见,或者我真的不擅长用谷歌搜索正确的帮助,哈哈

<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>