【问题标题】:How can I fix error "_helpers.pageUrl is not a function:" in my KeystoneJS project?如何在我的 KeystoneJS 项目中修复错误“_helpers.pageUrl 不是函数:”?
【发布时间】:2019-08-29 04:18:10
【问题描述】:

我有导游网站,我想在那里添加一些旅行。

我创建了两个模型:Tour.js 和 TourCategory.js(类似于 Post.js 和 PostCategory.js)。然后我必须创建两条路线:tours.js 和 tour.js(对于帖子也类似)。当我添加助手时,我发现了错误:_helpers.pageUrl is not a function

我该如何解决?

我试图找出是否有人有同样的问题,但一无所获。 我不知道如何修复它,因为在添加新助手之前我没有更改代码 (helpers/index.js) 中的任何内容。

我的 helpers/index.js 片段:

_helpers.tourUrl = function (categorySlug, tourSlug, options) {
    return ('/tours/' + categorySlug + tourSlug);
};

_helpers.categoryUrl = function (categorySlug, options) {
    return ('/tours/' + categorySlug);
};

_helpers.paginationNavigation = function (pages, currentPage, totalPages, options) {
    var html = '';

    _.each(pages, function (page, ctr) {

        var pageText = page;
        var isActivePage = ((page === currentPage) ? true : false);
        var liClass = ((isActivePage) ? ' class="active"' : '');

        if (page === '...') {

            page = ((ctr) ? totalPages : 1);
        }

        var pageUrl = _helpers.pageUrl(page);

        html += '<li' + liClass + '>' + linkTemplate({ url: pageUrl, text: pageText }) + '</li>\n';
    });
        return html;
}

我预计该页面将在没有任何错误的情况下加载。

【问题讨论】:

    标签: javascript node.js mongodb keystonejs


    【解决方案1】:

    因为在您的帮助文件中,您没有创建任何名称为pageUrl 的函数并尝试在另一个函数_helpers.pageUrl() 中访问。

    你需要像这样添加一个名称为pageUrl的函数(如果不存在)

    const _helpers = {};
    
    _helpers.pageUrl = (input) => {
         try {
             // ... Your business logic
             return true; // It's up to you 
         } catch (err) {
            console.log(err);
            throw err;
         }
    };
    
    module.exports = _helpers
    
    

    【讨论】:

    • 谢谢,但是你能给我一个这个函数的例子吗? (对不起,我是 KeystoneJS 的初学者)
    • 你需要在_helpers对象中创建一个名为pageUrl的函数
    猜你喜欢
    • 2020-08-07
    • 2019-12-25
    • 2019-10-22
    • 2021-06-12
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2019-09-21
    相关资源
    最近更新 更多