簡體   English   中英

模塊在開發中有效,但在 heroku 中無效:nodejs 和 hbs 車把

[英]Module works in development but not in heroku: nodejs and hbs handlebars

更新的問題:

在本地開發中,模塊正在工作。 當我推送到 heroku 時,一個模塊看起來不起作用,它沒有將變量傳遞給助手。

我的助手文件夾:

ad.helper.js
car.helper.js
pagination.helper.js
urlPagination.helper.js

pagination.helper.js 包含:

const hbs = require('hbs');
const paginate = require('handlebars-paginate');
hbs.registerHelper('paginate', paginate);

需要一些警報('halndlebars-paginate')

module "/home/santiago/code/folder/projectName/node_modules/handlebars-paginate/index"
Could not find a declaration file for module 'handlebars-paginate'. '/home/santiago/code/folder/projectName/node_modules/handlebars-paginate/index.js' implicitly has an 'any' type.
  Try `npm install @types/handlebars-paginate` if it exists or add a new declaration (.d.ts) file containing `declare module 'handlebars-paginate';`ts(7016)

查看 helper 所在的位置 => lis.hbs,其中包含:

 {{#paginate pagination type="first"}}
      <a {{#if disabled}}class="disabled"{{/if}} 
      href="{{#urlPagination parentCategory state category n searchWord vendor ageLow ageHigh vendorType priceLow priceHigh brand carmodel km yearLow yearHigh ccLow ccHigh}}{{/urlPagination}}"><li>Primera</li></a>
    {{/paginate}}
    {{#paginate pagination type="previous"}}
      <a {{#if disabled}}class="disabled"{{/if}} 
      href="{{#urlPagination parentCategory state category n searchWord vendor ageLow ageHigh vendorType priceLow priceHigh brand carmodel km yearLow yearHigh ccLow ccHigh}}{{/urlPagination}}"><li>Menos</li></a>
    {{/paginate}}
    {{#paginate pagination type="middle" limit="5"}}
      <a {{#if active}}class="active"{{/if}} href="{{#urlPagination parentCategory state category n searchWord vendor ageLow ageHigh vendorType priceLow priceHigh brand carmodel km yearLow yearHigh ccLow ccHigh}}{{/urlPagination}}"><li >{{n}}</li></a>
    {{/paginate}}
    {{#paginate pagination type="next"}}
      <a {{#if disabled}}class="disabled"{{/if}} href="{{#urlPagination parentCategory state category n searchWord vendor ageLow ageHigh vendorType priceLow priceHigh brand carmodel km yearLow yearHigh ccLow ccHigh}}{{/urlPagination}}"><li>Más</li></a>
    {{/paginate}}
    {{#paginate pagination type="last"}}
      <a {{#if disabled}}class="disabled"{{/if}} href="{{#urlPagination parentCategory state category n searchWord vendor ageLow ageHigh vendorType priceLow priceHigh brand carmodel km yearLow yearHigh ccLow ccHigh}}{{/urlPagination}}"><li>Última</li></a>
    {{/paginate}}

當我在 controller 中 console.log parentCategory 時,我看到了它,但是如果我在 urlpagination.helper 中 console.log 它是未定義的。

所以,我認為這個模塊在生產(heroku)中不起作用,但我不明白為什么它在開發中。

老問題:這個模塊發生了一些事情,我幾天都想不通。

是這個 npm package:

https://github.com/olalonde/handlebars-paginate

我正在使用車把。 在我的助手文件夾中,我有:

const hbs = require('hbs');
const paginate = require('handlebars-paginate');
hbs.registerHelper('paginate', paginate);

在我的文本編輯器中,我可以看到這個警告:

Could not find a declaration file for module 'handlebars-paginate'. '/home/santiago/code/folder/nameProject/node_modules/handlebars-paginate/index.js' implicitly has an 'any' type.
  Try `npm install @types/handlebars-paginate` if it exists or add a new declaration (.d.ts) file containing `declare module 'handlebars-paginate';`

我試圖做這兩種解決方案。

第一個,拋出錯誤,所以我創建了 adts 文件,並警告 go 離開。 之后,仍然無法在 heroku(生產模式)下工作。

我沒有使用 typescript,我認為第二個解決方案不適合我,為什么會出現這個警告? (注意:當我創建這個文件時,它沒有上傳到 heroku,即使我多次嘗試尋找 gitignore ......)

有趣的是,它在開發(本地主機)中運行得非常完美,但在 heroku 中,它不僅僅適用於這個模塊 - 分頁。

我究竟做錯了什么? 為什么看起來像 TS 錯誤?

我是個新手。 請幫忙,我非常接近完成我的第一個項目!

我解決了。 我認為這是一個非常愚蠢的錯誤,關於require modules 中的路徑 更准確地說,問題出在這里:

我的助手文件夾:

ad.helper.js
car.helper.js
pagination.helper.js
urlPagination.helper.js

分頁模塊,在 pagination.helper.js 中是必需的:

const hbs = require('hbs');
const paginate = require('handlebars-paginate');
hbs.registerHelper('paginate', paginate);

require('handlebars-paginate') 通過 node_modules => index.js:

module.exports = function(pagination, options) {
  var type = options.hash.type || 'middle';
  var ret = '';
  var pageCount = Number(pagination.pageCount);
  var page = Number(pagination.page);
  var limit;
  var state = Number(pagination.state);
  var parentCategory = Number(pagination.parentCategory);
  var category = Number(pagination.category);
  var searchWord = pagination.searchWord;
  var vendor = pagination.vendor;
  var ageLow = Number(pagination.ageLow);
  var ageHigh = Number(pagination.ageHigh);
  var vendorType = pagination.vendorType;
  var priceLow = pagination.priceLow;
  var priceHigh = pagination.priceHigh;
  var brand = pagination.brand;
  var carmodel = pagination.carmodel;
  var yearLow = pagination.yearLow;
  var yearHigh = pagination.yearHigh;
  var km = pagination.km;
  var ccLow = pagination.ccLow;
  var ccHigh = pagination.ccHigh;


  if (options.hash.limit) limit = +options.hash.limit;

  //page pageCount
  var newContext = {};
  switch (type) {
    case 'middle':
      if (typeof limit === 'number') {
        var i = 0;
        var leftCount = Math.ceil(limit / 2) - 1;
        var rightCount = limit - leftCount - 1;
        if (page + rightCount > pageCount)
          leftCount = limit - (pageCount - page) - 1;
        if (page - leftCount < 1)
          leftCount = page - 1;
        var start = page - leftCount;

        while (i < limit && i < pageCount) {
          newContext = { n: start, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh };
          if (start === page) newContext.active = true;
          ret = ret + options.fn(newContext);
          start++;
          i++;
        }
      }
      else {
        for (var i = 1; i <= pageCount; i++) {
          newContext = { n: i, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh };
          if (i === page) newContext.active = true;
          ret = ret + options.fn(newContext);
        }
      }
      break;
    case 'previous':
      if (page === 1) {
        newContext = { disabled: true, n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: page - 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
    case 'next':
      newContext = {};
      if (page === pageCount) {
        newContext = { disabled: true, n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: page + 1 , state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh}
      }
      ret = ret + options.fn(newContext);
      break;
    case 'first':
      if (page === 1) {
        newContext = { disabled: true, n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
    case 'last':
      if (page === pageCount) {
        newContext = { disabled: true, n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
  }

  return ret;
};

我不知道為什么,這適用於本地開發,但不適用於生產(heroku)......如果有人知道回答,我想理解它。

因此,通過將此 function 添加到我的助手中來修復,例如:

const hbs = require('hbs');
// modified for production env - 
// const paginate = require('handlebars-paginate');
hbs.registerHelper('paginate', function(pagination, options) {
  var type = options.hash.type || 'middle';
  var ret = '';
  var pageCount = Number(pagination.pageCount);
  var page = Number(pagination.page);
  var limit;
  var state = Number(pagination.state);
  var parentCategory = Number(pagination.parentCategory);
  var category = Number(pagination.category);
  var searchWord = pagination.searchWord;
  var vendor = pagination.vendor;
  var ageLow = Number(pagination.ageLow);
  var ageHigh = Number(pagination.ageHigh);
  var vendorType = pagination.vendorType;
  var priceLow = pagination.priceLow;
  var priceHigh = pagination.priceHigh;
  var brand = pagination.brand;
  var carmodel = pagination.carmodel;
  var yearLow = pagination.yearLow;
  var yearHigh = pagination.yearHigh;
  var km = pagination.km;
  var ccLow = pagination.ccLow;
  var ccHigh = pagination.ccHigh;


  if (options.hash.limit) limit = +options.hash.limit;

  //page pageCount
  var newContext = {};
  switch (type) {
    case 'middle':
      if (typeof limit === 'number') {
        var i = 0;
        var leftCount = Math.ceil(limit / 2) - 1;
        var rightCount = limit - leftCount - 1;
        if (page + rightCount > pageCount)
          leftCount = limit - (pageCount - page) - 1;
        if (page - leftCount < 1)
          leftCount = page - 1;
        var start = page - leftCount;

        while (i < limit && i < pageCount) {
          newContext = { n: start, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh };
          if (start === page) newContext.active = true;
          ret = ret + options.fn(newContext);
          start++;
          i++;
        }
      }
      else {
        for (var i = 1; i <= pageCount; i++) {
          newContext = { n: i, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh };
          if (i === page) newContext.active = true;
          ret = ret + options.fn(newContext);
        }
      }
      break;
    case 'previous':
      if (page === 1) {
        newContext = { disabled: true, n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: page - 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
    case 'next':
      newContext = {};
      if (page === pageCount) {
        newContext = { disabled: true, n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: page + 1 , state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh}
      }
      ret = ret + options.fn(newContext);
      break;
    case 'first':
      if (page === 1) {
        newContext = { disabled: true, n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: 1, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
    case 'last':
      if (page === pageCount) {
        newContext = { disabled: true, n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      else {
        newContext = { n: pageCount, state: state, parentCategory:parentCategory, category:category, searchWord:searchWord, vendor:vendor, ageLow:ageLow, ageHigh:ageHigh, vendorType:vendorType, priceLow:priceLow, priceHigh:priceHigh, brand:brand, carmodel:carmodel, km:km, yearLow:yearLow, yearHigh:yearHigh, ccLow:ccLow, ccHigh:ccHigh }
      }
      ret = ret + options.fn(newContext);
      break;
  }

  return ret;
})

我之前嘗試用親戚路徑修復:

./
../
node_modules/

但它不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM